#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("inline") #include using namespace std; #define MD (998244353U) #define MD_PRIMITIVE_ROOT (3U) #define PI 3.14159265358979323846 template struct cLtraits_identity{ using type = T; } ; template using cLtraits_try_make_signed = typename conditional< is_integral::value, make_signed, cLtraits_identity >::type; template struct cLtraits_common_type{ using tS = typename cLtraits_try_make_signed::type; using tT = typename cLtraits_try_make_signed::type; using type = typename common_type::type; } ; void*wmem; char memarr[96000000]; template inline auto max_L(S a, T b) -> typename cLtraits_common_type::type{ return (typename cLtraits_common_type::type) a >= (typename cLtraits_common_type::type) b ? a : b; } template inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } struct Modint{ unsigned val; Modint(){ val=0; } Modint(int a){ val = ord(a); } Modint(unsigned a){ val = ord(a); } Modint(long long a){ val = ord(a); } Modint(unsigned long long a){ val = ord(a); } inline unsigned ord(unsigned a){ return a%MD; } inline unsigned ord(int a){ a %= (int)MD; if(a < 0){ a += MD; } return a; } inline unsigned ord(unsigned long long a){ return a%MD; } inline unsigned ord(long long a){ a %= (int)MD; if(a < 0){ a += MD; } return a; } inline unsigned get(){ return val; } inline Modint &operator++(){ val++; if(val >= MD){ val -= MD; } return *this; } inline Modint &operator--(){ if(val == 0){ val = MD - 1; } else{ --val; } return *this; } inline Modint operator++(int a){ Modint res(*this); val++; if(val >= MD){ val -= MD; } return res; } inline Modint operator--(int a){ Modint res(*this); if(val == 0){ val = MD - 1; } else{ --val; } return res; } inline Modint &operator+=(Modint a){ val += a.val; if(val >= MD){ val -= MD; } return *this; } inline Modint &operator-=(Modint a){ if(val < a.val){ val = val + MD - a.val; } else{ val -= a.val; } return *this; } inline Modint &operator*=(Modint a){ val = ((unsigned long long)val*a.val)%MD; return *this; } inline Modint &operator/=(Modint a){ return *this *= a.inverse(); } inline Modint operator+(Modint a){ return Modint(*this)+=a; } inline Modint operator-(Modint a){ return Modint(*this)-=a; } inline Modint operator*(Modint a){ return Modint(*this)*=a; } inline Modint operator/(Modint a){ return Modint(*this)/=a; } inline Modint operator+(int a){ return Modint(*this)+=Modint(a); } inline Modint operator-(int a){ return Modint(*this)-=Modint(a); } inline Modint operator*(int a){ return Modint(*this)*=Modint(a); } inline Modint operator/(int a){ return Modint(*this)/=Modint(a); } inline Modint operator+(long long a){ return Modint(*this)+=Modint(a); } inline Modint operator-(long long a){ return Modint(*this)-=Modint(a); } inline Modint operator*(long long a){ return Modint(*this)*=Modint(a); } inline Modint operator/(long long a){ return Modint(*this)/=Modint(a); } inline Modint operator-(void){ Modint res; if(val){ res.val=MD-val; } else{ res.val=0; } return res; } inline operator bool(void){ return val!=0; } inline operator int(void){ return get(); } inline operator long long(void){ return get(); } inline Modint inverse(){ int a = val; int b = MD; int u = 1; int v = 0; int t; Modint res; while(b){ t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } if(u < 0){ u += MD; } res.val = u; return res; } inline Modint pw(unsigned long long b){ Modint a(*this); Modint res; res.val = 1; while(b){ if(b&1){ res *= a; } b >>= 1; a *= a; } return res; } inline bool operator==(int a){ return ord(a)==val; } inline bool operator!=(int a){ return ord(a)!=val; } } ; inline Modint operator+(int a, Modint b){ return Modint(a)+=b; } inline Modint operator-(int a, Modint b){ return Modint(a)-=b; } inline Modint operator*(int a, Modint b){ return Modint(a)*=b; } inline Modint operator/(int a, Modint b){ return Modint(a)/=b; } inline Modint operator+(long long a, Modint b){ return Modint(a)+=b; } inline Modint operator-(long long a, Modint b){ return Modint(a)-=b; } inline Modint operator*(long long a, Modint b){ return Modint(a)*=b; } inline Modint operator/(long long a, Modint b){ return Modint(a)/=b; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline void rd(long long &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(Modint x){ int i; i = (int)x; wt_L(i); } template inline S chmax(S &a, T b){ if(a 2){ n1 = n / 4; n2 = n1 + n1; n3 = n1 + n2; for(i=(0);i<(n1);i++){ w1 = fft_pnt(cos(i*theta),-sin(i*theta)); w2 = w1*w1; w3 = w1*w2; for(j=(0);j<(step);j++){ a = x[j+step*i]; b = x[j+step*(i+n1)]; c = x[j+step*(i+n2)]; d = x[j+step*(i+n3)]; aa = a + c; bb = a - c; cc = b + d; dd = b - d; tmp = dd.y; dd.y = dd.x; dd.x = -tmp; y[j+step*(4*i )] = aa + cc; y[j+step*(4*i+1)] = w1*(bb - dd); y[j+step*(4*i+2)] = w2*(aa - cc); y[j+step*(4*i+3)] = w3*(bb + dd); } } n /= 4; step *= 4; theta *= 4; swap(x,y); } if(n==2){ for(i=(0);i<(step);i++){ y[i] = x[i] + x[i+step]; y[i+step] = x[i] - x[i+step]; } n /= 2; step *= 2; theta *= 2; swap(x,y); } for(i=(0);i<(step);i++){ y[i] = x[i]; } } void fftinv(int n, fft_pnt x[], void *mem = wmem){ int i; int j; int n1; int n2; int n3; int step = 1; double theta = 2*PI / n; double tmp; fft_pnt w1; fft_pnt w2; fft_pnt w3; fft_pnt a; fft_pnt b; fft_pnt c; fft_pnt d; fft_pnt aa; fft_pnt bb; fft_pnt cc; fft_pnt dd; fft_pnt*y = (fft_pnt*)mem; while(n > 2){ n1 = n / 4; n2 = n1 + n1; n3 = n1 + n2; for(i=(0);i<(n1);i++){ w1 = fft_pnt(cos(i*theta),sin(i*theta)); w2 = w1*w1; w3 = w1*w2; for(j=(0);j<(step);j++){ a = x[j+step*i]; b = x[j+step*(i+n1)]; c = x[j+step*(i+n2)]; d = x[j+step*(i+n3)]; aa = a + c; bb = a - c; cc = b + d; dd = b - d; tmp = dd.y; dd.y = dd.x; dd.x = -tmp; y[j+step*(4*i )] = aa + cc; y[j+step*(4*i+1)] = w1*(bb + dd); y[j+step*(4*i+2)] = w2*(aa - cc); y[j+step*(4*i+3)] = w3*(bb - dd); } } n /= 4; step *= 4; theta *= 4; swap(x,y); } if(n==2){ for(i=(0);i<(step);i++){ y[i] = x[i] + x[i+step]; y[i+step] = x[i] - x[i+step]; } n /= 2; step *= 2; theta *= 2; swap(x,y); } for(i=(0);i<(step);i++){ y[i] = x[i]; } } void convolution_L(double A[], int As, double B[], int Bs, double res[], int Rs, void *mem = wmem){ int i; int n; int n2; double mul; fft_pnt*a; fft_pnt*b; n =max_L(As+Bs, Rs); for(n2=1;n2 2){ n1 = n / 4; n2 = n1 + n1; n3 = n1 + n2; w1.val = 1; for(i=(0);i<(n1);i++){ w2 = w1*w1; w3 = w1*w2; for(j=(0);j<(step);j++){ a = x[j+step*i]; b = x[j+step*(i+n1)]; c = x[j+step*(i+n2)]; d = x[j+step*(i+n3)]; aa = a + c; bb = a - c; cc = b + d; dd = (b - d) * tmp; y[j+step*(4*i )] = aa + cc; y[j+step*(4*i+1)] = w1*(bb - dd); y[j+step*(4*i+2)] = w2*(aa - cc); y[j+step*(4*i+3)] = w3*(bb + dd); } w1 *= root; } n /= 4; step *= 4; root *= root; root *= root; swap(x,y); } if(n==2){ for(i=(0);i<(step);i++){ y[i] = x[i] + x[i+step]; y[i+step] = x[i] - x[i+step]; } n /= 2; step *= 2; root *= root; swap(x,y); } for(i=(0);i<(step);i++){ y[i] = x[i]; } } void fftinv(int n, Modint x[], Modint root = MD_PRIMITIVE_ROOT, void *mem = wmem){ int i; int j; int n1; int n2; int n3; int step = 1; Modint w1; Modint w2; Modint w3; Modint a; Modint b; Modint c; Modint d; Modint aa; Modint bb; Modint cc; Modint dd; Modint tmp; Modint*y; walloc1d(&y, n, &mem); root = root.inverse(); tmp = root.pw((MD-1)/4); root = root.pw((MD-1)/n); while(n > 2){ n1 = n / 4; n2 = n1 + n1; n3 = n1 + n2; w1.val = 1; for(i=(0);i<(n1);i++){ w2 = w1*w1; w3 = w1*w2; for(j=(0);j<(step);j++){ a = x[j+step*i]; b = x[j+step*(i+n1)]; c = x[j+step*(i+n2)]; d = x[j+step*(i+n3)]; aa = a + c; bb = a - c; cc = b + d; dd = (b - d) * tmp; y[j+step*(4*i )] = aa + cc; y[j+step*(4*i+1)] = w1*(bb + dd); y[j+step*(4*i+2)] = w2*(aa - cc); y[j+step*(4*i+3)] = w3*(bb - dd); } w1 *= root; } n /= 4; step *= 4; root *= root; root *= root; swap(x,y); } if(n==2){ for(i=(0);i<(step);i++){ y[i] = x[i] + x[i+step]; y[i+step] = x[i] - x[i+step]; } n /= 2; step *= 2; root *= root; swap(x,y); } for(i=(0);i<(step);i++){ y[i] = x[i]; } } void convolution_L(Modint A[], int As, Modint B[], int Bs, Modint res[], int Rs, Modint root = MD_PRIMITIVE_ROOT, void *mem = wmem){ int i; int n; int n2; Modint*a; Modint*b; Modint r; n =max_L(As+Bs, Rs); for(n2=1;n2 ind; Modint H(long long a, int b){ int i; Modint x; Modint y; if(a == b && b == 0){ return 1; } if(a <= 0 || b < 0){ return 0; } x = y = 1; for(i=(0);i<(b);i++){ x *= a+i; y *= i+1; } return x / y; } int main(){ int i, q; wmem = memarr; int mx = -1; int sz = 0; Modint res; rd(N); rd(Q); { int Q5VJL1cS; for(Q5VJL1cS=(0);Q5VJL1cS<(Q);Q5VJL1cS++){ rd(K[Q5VJL1cS]); rd(A[Q5VJL1cS]); rd(B[Q5VJL1cS]); rd(S[Q5VJL1cS]); rd(T[Q5VJL1cS]); } } for(i=(0);i<(Q);i++){ int j; for(j=(0);j<(3001);j++){ ok[i][j] = 1; } } for(i=(0);i<(Q);i++){ if(ind.count(K[i])){ K[i] = ind[K[i]]; continue; } K[i] = ind[K[i]] = sz++; } for(q=(0);q<(Q);q++){ chmax(mx, K[q]+1); for(i=(A[q]);i<(B[q]+1);i++){ ok[K[q]][i] = 0; } for(i=(0);i<(3001);i++){ dp[i] = ok[0][i]; } for(i=(1);i<(mx);i++){ int j; convolution_L(dp, 3001, ok[i], 3001, nx, 3001); for(j=(0);j<(3001);j++){ dp[j] = nx[j]; } } res = 0; for(i=(0);i<(T[q]+1);i++){ if(dp[i]){ res += H(N-mx+1, T[q]-i) * dp[i]; res -= H(N-mx+1, S[q]-i-1) * dp[i]; } } wt_L(res); wt_L('\n'); } return 0; } // cLay version 20210611-1 [beta] // --- original code --- // #define MD 998244353 // ll N, K[100]; // int Q, A[], B[], S[], T[]; // Modint ok[100][3001]; // Modint dp[3001], nx[3001]; // map ind; // // Modint H(ll a, int b){ // int i; // Modint x, y; // if(a == b == 0) return 1; // if(a <= 0 || b < 0) return 0; // x = y = 1; // rep(i,b){ // x *= a+i; // y *= i+1; // } // return x / y; // } // // { // int mx = -1, sz = 0; // Modint res; // rd(N,Q,(K,A,B,S,T)(Q)); // rep(i,Q) rep(j,3001) ok[i][j] = 1; // // rep(i,Q){ // if(ind.count(K[i])) K[i] = ind[K[i]], continue; // K[i] = ind[K[i]] = sz++; // } // // rep(q,Q){ // mx >?= K[q]+1; // rep(i,A[q],B[q]+1) ok[K[q]][i] = 0; // rep(i,3001) dp[i] = ok[0][i]; // rep(i,1,mx){ // convolution(dp, 3001, ok[i], 3001, nx, 3001); // rep(j,3001) dp[j] = nx[j]; // } // res = 0; // rep(i,T[q]+1) if(dp[i]){ // res += H(N-mx+1, T[q]-i) * dp[i]; // res -= H(N-mx+1, S[q]-i-1) * dp[i]; // } // wt(res); // } // }