#include using namespace std; long long mod = 998244353; //入力が必ず-mod= mod) v -= mod; return *this; } mint operator-=(const mint b){ v -= b.v; if(v < 0) v += mod; return *this; } mint operator*=(const mint b){v = v*b.v%mod; return *this;} mint operator/=(mint b){ if(b == 0) assert(false); int left = mod-2; while(left){if(left&1) *this *= b; b *= b; left >>= 1;} return *this; } mint operator++(){*this += 1; return *this;} mint operator--(){*this -= 1; return *this;} mint operator++(int){*this += 1; return *this;} mint operator--(int){*this -= 1; return *this;} bool operator==(const mint b){return v == b.v;} bool operator!=(const mint b){return v != b.v;} bool operator>(const mint b){return v > b.v;} bool operator>=(const mint b){return v >= b.v;} bool operator<(const mint b){return v < b.v;} bool operator<=(const mint b){return v <= b.v;} mint pow(long long n){ mint ret = 1,p = v; if(n < 0) p = p.inv(),n = -n; while(n){ if(n&1) ret *= p; p *= p; n >>= 1; } return ret; } mint inv(){return mint(1)/v;} }; class UnionFind{ private: vector par,siz; public: UnionFind(int N){ par.resize(N,-1); siz.resize(N,1); } int root(int x){ //連結成分の代表頂点を返す. if(par.at(x) == -1) return x; else return par.at(x) = root(par.at(x)); } bool unite(int u, int v){ //u,vを連結する 連結してた->false,した->trueを返す. u = root(u),v = root(v); if(u == v) return false; if(siz.at(u) < siz.at(v)) swap(u,v); //Union by size. par.at(v) = u; siz.at(u) += siz.at(v); return true; } bool issame(int u, int v){ //同じ連結成分ならtrue. if(root(u) == root(v)) return true; else return false; } int size(int pos){return siz.at(root(pos));} //posの連結成分の大きさを返す. }; long long inv(vector P){ int N = P.size(); long long ret = 0; for(int i=0; iP.at(k); return ret; } using SS = mint; class SegmentTree{ public: int siz = -1,n = -1; vector dat; SS op(SS a, SS b){return a*b;} SS e(){return 1;} void renew (SS &a,SS x){ a = op(a,x); //a = x; //set(pos,x)で可能. //その他. } SegmentTree(int N){init(N);} SegmentTree(const vector &A){//長さ配列サイズに合わせる. siz = 1; n = A.size(); while(siz < n) siz *= 2; dat.resize(siz*2,e()); for(int i=0; i0; i--) dat.at(i) = op(dat.at(i*2),dat.at(i*2+1)); } void init(int N){ //全要素単位元に初期化. siz = 1; n = N; while(siz < n) siz *= 2; dat.assign(siz*2,e()); } void init(const vector &A){//長さ配列サイズに合わせる. siz = 1; n = A.size(); dat.resize(siz*2,e()); for(int i=0; i0; i--) dat.at(i) = op(dat.at(i*2),dat.at(i*2+1)); } void set(int pos,SS x){ pos = pos+siz; dat.at(pos) = x; while(pos != 1){ pos = pos/2; dat.at(pos) = op(dat.at(pos*2),dat.at(pos*2+1)); } } void update(int pos,SS x){ pos = pos+siz; renew(dat.at(pos),x); while(pos != 1){ pos = pos/2; dat.at(pos) = op(dat.at(pos*2),dat.at(pos*2+1)); } } SS findans(int l, int r){ SS retl = e(),retr = e(); l += siz,r += siz; while(l < r){ if(l&1) retl = op(retl,dat.at(l++)); if(r&1) retr = op(dat.at(--r),retr); l >>= 1; r >>= 1; } return op(retl,retr); } SS get(int pos){return dat.at(pos+siz);} SS rangeans(int l, int r){return findans(l,r);} SS allrange(){return dat.at(1);} //rightは) leftは[で 渡す&返す. int maxright(const function f,int l = 0){ //fを満たさない最小の箇所を返す なければn. l += siz; int r = n+siz; vector ls,rs; while(l < r){ if(l&1) ls.push_back(l++); if(r&1) rs.push_back(--r); l >>= 1; r >>= 1; } SS okl = e(); for(int i=0; i=0; i--){ l = rs.at(i); SS now = op(okl,dat.at(l)); if(!f(now)){ while(l < siz){ l <<= 1; now = op(okl,dat.at(l)); if(f(now)){okl = now; l++;} } return l-siz; } okl = now; } return n; } int minleft(const function f,int r = -1){ //fを満たす最小の箇所を返す なければ0. if(r == -1) r = n; int l = siz; r += siz; vector ls,rs; while(l < r){ if(l&1) ls.push_back(l++); if(r&1) rs.push_back(--r); l >>= 1; r >>= 1; } SS okr = e(); for(int i=0; i=0; i--){ r = ls.at(i); SS now = op(dat.at(r),okr); if(!f(now)){ while(r < siz){ r <<= 1; r++; now = op(dat.at(r),okr); if(f(now)){okr = now; r--;} } return r+1-siz; } okr = now; } return 0; } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; cin >> mod; if(mod == 1){ while(T--) cout << "0\n"; return 0; } int n = 5000000; vector fac(n+1,1); for(int i=1; i<=n; i++) fac.at(i) = fac.at(i-1)*i; vector give(n+1); for(int i=0; i<=n; i++) give.at(i) = i%mod; SegmentTree Seg(give); //OEIS最高!→え~何が違うの→mod合成数の時facの逆元が悪さしてる. //連結成分サイズ2 逆順列=revとなる数 A037224->A001813 2n!/n!->(n+1)*(n+2)*...*2nなのでセグ木. //連結成分サイズ2 それ以外 A000898. //連結成分サイズ8 A000899*8. n!-2Xn-Y[n/2]+2*Two[n/2] vector Two(n/2+1),Eight(n+1),X(n+1),Y(n/2+1); Two.at(0) = 1; Two.at(1) = 2%mod; X.at(0) = 1; X.at(1) = 1; X.at(2) = 2%mod; for(int i=2; i<=n/2; i++) Two.at(i) = (Two.at(i-1)+Two.at(i-2)*(i-1))*2; for(int i=3; i<=n; i++) X.at(i) = X.at(i-1)+X.at(i-2)*(i-1); mint p2 = 1; for(int i=0; i<=n/2; i++) Y.at(i) = fac.at(i)*p2,p2 += p2; for(int i=1; i<=n; i++) Eight.at(i) = fac.at(i)-X.at(i)*2-Y.at(i/2)+Two.at(i/2)*2; while(T--){ int N; cin >> N; if(N == 1){cout << 1 << "\n"; continue;} mint two = Two.at(N/2),answer = 0,eight = Eight.at(N),four = 0; if(N%4 == 0 || N%4 == 1){ int M = N/4; two += Seg.rangeans(M+1,2*M+1); } four = fac.at(N)-two-eight; answer = two*2+four*4+eight*8; answer.v %= mod; cout << answer.v << "\n"; continue; long long f = 1; for(int i=1; i<=N; i++) f *= i; UnionFind Z(f); map,long long> name; vector P(N); iota(P.begin(),P.end(),0); do{ name[P] = name.size(); }while(next_permutation(P.begin(),P.end())); iota(P.begin(),P.end(),0); vector Q(N); int idx = -1; do{ idx++; reverse(P.begin(),P.end()); Z.unite(idx,name[P]); reverse(P.begin(),P.end()); for(int i=0; i ap; for(auto [K,v] : name){ ap[Z.size(v)]++; if(Z.size(v) == 2) continue; //continue; vector S(N); for(int i=0; i B(N); } //cout << N << endl; long long ans = 0; for(auto [k,v] : ap) cout << k << "->" << v << endl,ans += k*v; cout << ans << endl; cout << endl; } }