#include using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; ll M; cin >> T >> M; vector Ns(T); int maxN = 0; for(int i = 0; i < T; i++){ cin >> Ns[i]; maxN = max(maxN, Ns[i]); } // 1) factorial mod M vector fact(maxN+1); fact[0] = 1; for(int i = 1; i <= maxN; i++){ fact[i] = fact[i-1] * i % M; } // 2) involutions a[n]: a[0]=1, a[1]=1, a[n]=a[n-1]+(n-1)*a[n-2] vector a(maxN+1); a[0] = 1; if(maxN >= 1) a[1] = 1; for(int n = 2; n <= maxN; n++){ a[n] = (a[n-1] + (ll)(n-1) * a[n-2]) % M; } // 3) pow2 up to floor(maxN/2) for c[n] int halfN = maxN/2; vector pow2(halfN+1); pow2[0] = 1; for(int i = 1; i <= halfN; i++){ pow2[i] = (pow2[i-1] * 2) % M; } // 4) P[m] = product_{k=1..m}(4k-2) mod M for d[n] int quarterN = maxN/4; vector P(quarterN+1); P[0] = 1; for(int m = 1; m <= quarterN; m++){ // 4*m - 2 fits in ll P[m] = (P[m-1] * (4LL*m - 2)) % M; } // 5) H[m] = number of involutions that are also 180°-symmetric on floor(n/2) pairs // H[0]=1, H[1]=2, H[m]=2*H[m-1] + 2*(m-1)*H[m-2] vector H(halfN+1); H[0] = 1; if(halfN >= 1) H[1] = 2 % M; for(int m = 2; m <= halfN; m++){ H[m] = (2 * H[m-1] + 2LL*(m-1) * H[m-2]) % M; } // 6) 答えを計算 // c[n] = fact[floor(n/2)] * pow2[floor(n/2)] // d[n] = (n%4==0 || n%4==1) ? P[floor(n/4)] : 0 // h[n] = H[floor(n/2)] // S[n] = 8*fact[n] - 8*a[n] - 4*c[n] - 2*d[n] + 6*h[n] (mod M) // for(int n : Ns){ ll fn = fact[n]; ll an = a[n]; int m2 = n/2; ll cn = (fact[m2] * pow2[m2]) % M; int m4 = n/4; ll dn = (n%4==0 || n%4==1) ? P[m4] : 0; ll hn = H[m2]; ll S = 0; S = (S + 8LL * fn) % M; S = (S - 8LL * an % M + M) % M; S = (S - 4LL * cn % M + M) % M; S = (S - 2LL * dn % M + M) % M; S = (S + 6LL * hn) % M; cout << S << "\n"; } return 0; }