#include<bits/stdc++.h> using i64 = longlong; using u64 = unsignedlonglong; using i128 = __int128; #define int i64 #define pb push_back #define ep emplace #define eb emplace_back usingnamespace std; intread(int x = 0, int f = 0, char ch = getchar()){ while (ch < 48or57 < ch) f = ch == 45, ch = getchar(); while (48 <= ch and ch <= 57) x = x * 10 + ch - 48, ch = getchar(); return f ? -x : x; } #define debug(x) cout << #x << " = " << x << "\n"; #define vdebug(a) cout << #a << " = "; for (auto x : a) cout << x << " "; cout << "\n"; template <classT1, classT2> ostream &operator<<(ostream &os, const std::pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <classT> ostream &operator<<(ostream &os, const vector<T> &as) { constint sz = as.size(); os << "["; for (int i = 0; i < sz; i++) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template <classT> voidpv(T a, T b){ for (T i = a; i != b; i++) cerr << *i << " "; cerr << '\n'; } using pii = pair<int, int>; constint inf = 1e18; constint MOD = 998244383;
ostream &operator<<(ostream &os, i128 n) { if (n == 0) return os << 0; string s; while (n > 0) s += char('0' + n % 10), n /= 10; reverse(s.begin(), s.end()); return os << s; }
template <classT> constexpr T power(T a, i64 b){ T res {1}; for (; b; b /= 2, a *= a) if (b % 2) res *= a; return res; } constexpr i64 mul(i64 a, i64 b, i64 p){ i64 res = a * b - (i64)(1.L * a * b / p) * p; res %= p; if (res < 0) res += p; return res; } template <i64 P> structMInt { i64 x; constexprMInt() : x {0} {} constexprMInt(i64 x) : x {norm(x % getMod())} {} static i64 Mod; constexprstatic i64 getMod(){ return P > 0 ? P : Mod; } constexprstaticvoidsetMod(i64 Mod_){ Mod = Mod_; } constexpr i64 norm(i64 x)const{ if (x < 0) x += getMod(); if (x >= getMod()) x -= getMod(); return x; } constexpr i64 val()const{ return x; } constexpr MInt operator-() const { MInt res; res.x = norm(getMod() - x); return res; } constexpr MInt inv()const{ returnpower(*this, getMod() - 2); } constexpr MInt &operator*=(MInt rhs) & { if (getMod() < (1ULL << 31)) x = x * rhs.x % int(getMod()); else x = mul(x, rhs.x, getMod()); return *this; } constexpr MInt &operator+=(MInt rhs) & { x = norm(x + rhs.x); return *this; } constexpr MInt &operator-=(MInt rhs) & { x = norm(x - rhs.x); return *this; } constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); } friendconstexpr MInt operator*(MInt lhs, MInt rhs) { MInt res = lhs; res *= rhs; return res; } friendconstexpr MInt operator+(MInt lhs, MInt rhs) { MInt res = lhs; res += rhs; return res; } friendconstexpr MInt operator-(MInt lhs, MInt rhs) { MInt res = lhs; res -= rhs; return res; } friendconstexpr MInt operator/(MInt lhs, MInt rhs) { MInt res = lhs; res /= rhs; return res; } friendconstexpr std::istream &operator>>(std::istream &is, MInt &a) { i64 v; is >> v; a = MInt(v); return is; } friendconstexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); } friendconstexprbooloperator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); } friendconstexprbooloperator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); } friendconstexprbooloperator<(MInt lhs, MInt rhs) { return lhs.val() < rhs.val(); } }; template <> i64 MInt<0>::Mod = 998244353; constexprint P = 998244353; using Z = MInt<P>; using DZ = MInt<0>;
voidsolve(){ int p = read(); int k = (p + 118) / 119; Z ans = 0;
if (k >= 23) { int res = 0; Z t = power(Z(2), k); for (int i = (p - 1) / 4 * 4; i <= p - 1; i++) { res ^= i; } ans = Z(res) * (t * t + t); ans += Z(p) * Z(p - 2) + 2; } else { DZ::setMod(p); i128 res = 0; i128 t = 1ll << k; for (int i = 1; i < p; i++) { res ^= (DZ(i).inv().val() + t) * (i + t * t); } ans = res; }
cout << ans << '\n'; }
signedmain(){ for (int T = read(); T--; solve()); // solve(); return0; }