#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using mint = modint; constexpr int M = 5000010; mint Fact[M], DFact[M], dp2[M], dp234[M]; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t, m; cin >> t >> m; mint::set_mod(m); Fact[0] = 1; for (int i = 1; i < M; i++) { Fact[i] = i * Fact[i-1]; } DFact[0] = DFact[1] = 1; for (int i = 2; i < M; i++) { DFact[i] = i * DFact[i-2]; } dp2[0] = dp2[1] = 1; for (int i = 2; i < M; i++) { dp2[i] = dp2[i-1] + (i - 1) * dp2[i-2]; } dp234[0] = dp234[1] = 1; for (int i = 2; i < M; i++) { dp234[i] = 2 * dp234[i-2]; if (i >= 4) dp234[i] += ((i - 2) & ~1) * dp234[i-4]; } rep(_, t) { int n; cin >> n; if (n == 1) { cout << 1 % m << '\n'; continue; } // def r:=rev, q=p^-1 // {p,pr,rp,rpr,q,qr,rq,rqr} generated // 4 kinds of possible equalities: // 1. p=qr // 2. p=q // 3. p=rpr // 4. p=rqr // 1 <=> p^2=r => 3; p,p^3 generated // each 2,3,4 holds when the other two do mint c1, c2, c3, c4, c234, c0; if (n % 4 <= 1) { c1 = DFact[n / 2 - 1] * mint(2).pow(n / 4); } c2 = c4 = dp2[n]; c3 = DFact[n & ~1]; c234 = dp234[n]; c2 -= c234, c3 -= c1 + c234, c4 -= c234; c0 = Fact[n] - c1 - c2 - c3 - c4 - c234; mint ans = 8 * c0 + 2 * c1 + 4 * (c2 + c3 + c4) + 2 * c234; cout << ans.val() << '\n'; } }