#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class ModModMod { public:
long long findSum(vector <int> m, int R)
{
memo.assign(m.size()+1, -1);
return rec(m, 0, R);
}
LL rec(const vector<int>& m, int i, int R)
{
if(i == m.size())
return LL(1+R)*R/2;
return rec_memod(m, i)*(R/m[i]) + rec(m, i+1, R%m[i]);
}
vector<LL> memo;
LL rec_memod(const vector<int>& m, int i) {
if(memo[i] >= 0)
return memo[i];
return memo[i] = rec(m, i+1, m[i]-1);
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, ModModMod().findSum(m, R));}
int main(){
CASE(0)
int m_[] = {5,3,2};
vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_));
int R = 10;
long long _ = 4LL;
END
CASE(1)
int m_[] = {33, 15, 7, 10, 100, 9, 5};
vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_));
int R = 64;
long long _ = 92LL;
END
CASE(2)
int m_[] = {995,149,28,265,275,107,555,241,702,462,519,212,362,478,783,381,602,546,183,886,59,317,977,612,328,91,771,131};
vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_));
int R = 992363;
long long _ = 12792005LL;
END
CASE(3)
int m_[] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100};
vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_));
int R = 100;
long long _ = 4950LL;
END
CASE(4)
int m_[] = {2934};
vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_));
int R = 10000000;
long long _ = 14664070144LL;
END
/*
CASE(5)
int m_[] = ;
vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_));
int R = ;
long long _ = LL;
END
CASE(6)
int m_[] = ;
vector <int> m(m_, m_+sizeof(m_)/sizeof(*m_));
int R = ;
long long _ = LL;
END
*/
}
// END CUT HERE