#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++){ P[m] = (P[m-1] * (4LL*m - 2)) % M; } // 5) H[m] = involutions ∩ 180°-symmetric on m pairs 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) クエリごとに出力 for(int n : Ns){ if(n == 1){ // 1 つの順列の軌道サイズは 1 cout << 1 << "\n"; continue; } // n >= 2 のときは以下の公式を使う 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; }