Artifact 4dd19b13cd92c7f0e04f050a85e1d4caec207b9b
//-------------------------------------------------------------
// Modulo Arithmetics
//
// Verified by
// - TCO10 R3 LV3
// - SRM 545 LV2
//-------------------------------------------------------------
static const int MODVAL = 1000000007;
struct mint
{
int val;
mint():val(0){}
mint(int x):val(x%MODVAL) {}
mint(size_t x):val(x%MODVAL) {}
mint(LL x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; }
mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); }
mint operator+(mint x, mint y) { return x+=y; }
mint operator-(mint x, mint y) { return x-=y; }
mint operator*(mint x, mint y) { return x*=y; }
mint operator/(mint x, mint y) { return x/=y; }
vector<mint> FAC_(1,1);
mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; }
mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n-k)); }
// Pascal's triangle: if O(1) nCk is needed.
vector< vector<mint> > CP_(2001);
mint C(LL n, LL k) {
if(CP_[0].empty()) {
CP_[0].push_back(1);
for(int nn=1; nn<CP_.size(); ++nn)
for(int kk=0; kk<=nn; ++kk)
CP_[nn].push_back( (kk?CP_[nn-1][kk-1]:0) + (kk<nn?CP_[nn-1][kk]:0) );
}
return k<0 || n<k ? 0 : CP_[n][k];
}
/*
// MODVAL must be a prime!!
LL GSS(LL k, LL b, LL e) // k^b + k^b+1 + ... + k^e
{
if( b > e ) return 0;
if( k <= 1 ) return k*(e-b+1);
return DIV(SUB(POW(k, e+1), POW(k,b)), k-1);
}
LL Cpascal(LL n, LL k)
{
vector< vector<LL> > c(n+1, vector<LL>(k+1));
for(LL nn=1; nn<=n; ++nn)
for(LL kk=0; kk<=min(nn,k); ++kk)
c[nn][kk] = kk==0 || kk==nn ? 1 : ADD(c[nn-1][kk-1], c[nn-1][kk]);
return c[n][k];
}
vector< vector<LL> > MATMUL(vector< vector<LL> >& a, vector< vector<LL> >& b)
{
int N = a.size();
vector< vector<LL> > c(N, vector<LL>(N));
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
for(int k=0; k<N; ++k)
c[i][j] = ADD(c[i][j], MUL(a[i][k],b[k][j]));
return c;
}
// works for non-prime MODVAL
LL GEO(LL x_, LL e) // x^0 + x^1 + ... + x^e-1
{
vector< vector<LL> > v(2, vector<LL>(2));
vector< vector<LL> > x(2, vector<LL>(2));
v[0][0] = v[1][1] = 1;
x[0][0] = x_; x[0][1] = 0;
x[1][0] = 1 ; x[1][1] = 1;
for(;e;x=MATMUL(x,x),e>>=1)
if(e&1)
v = MATMUL(v, x);
return v[1][0];
}
// works for non-prime MODVAL
LL HYP(LL x_, LL e) // e x^0 + (e-1) x^1 + ... + 1 x^e-1 = GEO(x,1)+GEO(x,2)+...+GEO(x,e)
{
vector< vector<LL> > v(3, vector<LL>(3));
vector< vector<LL> > x(3, vector<LL>(3));
v[0][0] = v[1][1] = v[2][2] = 1;
x[0][0] = x_; x[0][1] = 0; x[0][2] = 0;
x[1][0] = 1 ; x[1][1] = 1; x[1][2] = 0;
x[2][0] = 0 ; x[2][1] = 1; x[2][2] = 1;
e++;
for(;e;x=MATMUL(x,x),e>>=1)
if(e&1)
v = MATMUL(v, x);
return v[2][0];
}
*/