#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx2") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int 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 void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } //////////////////////////////////////////////////////////////////////////////// template struct ModInt { static constexpr unsigned M = M_; unsigned x; constexpr ModInt() : x(0U) {} constexpr ModInt(unsigned x_) : x(x_ % M) {} constexpr ModInt(unsigned long long x_) : x(x_ % M) {} constexpr ModInt(int x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} constexpr ModInt(long long x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { x = (static_cast(x) * a.x) % M; return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// constexpr unsigned MO = 998244353U; constexpr unsigned MO2 = 2U * MO; constexpr int FFT_MAX = 23; using Mint = ModInt; constexpr Mint FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 911660635U, 372528824U, 929031873U, 452798380U, 922799308U, 781712469U, 476477967U, 166035806U, 258648936U, 584193783U, 63912897U, 350007156U, 666702199U, 968855178U, 629671588U, 24514907U, 996173970U, 363395222U, 565042129U, 733596141U, 267099868U, 15311432U}; constexpr Mint INV_FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 86583718U, 509520358U, 337190230U, 87557064U, 609441965U, 135236158U, 304459705U, 685443576U, 381598368U, 335559352U, 129292727U, 358024708U, 814576206U, 708402881U, 283043518U, 3707709U, 121392023U, 704923114U, 950391366U, 428961804U, 382752275U, 469870224U}; constexpr Mint FFT_RATIOS[FFT_MAX] = {911660635U, 509520358U, 369330050U, 332049552U, 983190778U, 123842337U, 238493703U, 975955924U, 603855026U, 856644456U, 131300601U, 842657263U, 730768835U, 942482514U, 806263778U, 151565301U, 510815449U, 503497456U, 743006876U, 741047443U, 56250497U, 867605899U}; constexpr Mint INV_FFT_RATIOS[FFT_MAX] = {86583718U, 372528824U, 373294451U, 645684063U, 112220581U, 692852209U, 155456985U, 797128860U, 90816748U, 860285882U, 927414960U, 354738543U, 109331171U, 293255632U, 535113200U, 308540755U, 121186627U, 608385704U, 438932459U, 359477183U, 824071951U, 103369235U}; // as[rev(i)] <- \sum_j \zeta^(ij) as[j] void fft(Mint *as, int n) { assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX); int m = n; if (m >>= 1) { for (int i = 0; i < m; ++i) { const unsigned x = as[i + m].x; // < MO as[i + m].x = as[i].x + MO - x; // < 2 MO as[i].x += x; // < 2 MO } } if (m >>= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned x = (prod * as[i + m]).x; // < MO as[i + m].x = as[i].x + MO - x; // < 3 MO as[i].x += x; // < 3 MO } prod *= FFT_RATIOS[__builtin_ctz(++h)]; } } for (; m; ) { if (m >>= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned x = (prod * as[i + m]).x; // < MO as[i + m].x = as[i].x + MO - x; // < 4 MO as[i].x += x; // < 4 MO } prod *= FFT_RATIOS[__builtin_ctz(++h)]; } } if (m >>= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned x = (prod * as[i + m]).x; // < MO as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x; // < 2 MO as[i + m].x = as[i].x + MO - x; // < 3 MO as[i].x += x; // < 3 MO } prod *= FFT_RATIOS[__builtin_ctz(++h)]; } } } for (int i = 0; i < n; ++i) { as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x; // < 2 MO as[i].x = (as[i].x >= MO) ? (as[i].x - MO) : as[i].x; // < MO } } // as[i] <- (1/n) \sum_j \zeta^(-ij) as[rev(j)] void invFft(Mint *as, int n) { assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX); int m = 1; if (m < n >> 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + m; ++i) { const unsigned long long y = as[i].x + MO - as[i + m].x; // < 2 MO as[i].x += as[i + m].x; // < 2 MO as[i + m].x = (prod.x * y) % MO; // < MO } prod *= INV_FFT_RATIOS[__builtin_ctz(++h)]; } m <<= 1; } for (; m < n >> 1; m <<= 1) { Mint prod = 1U; for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) { for (int i = i0; i < i0 + (m >> 1); ++i) { const unsigned long long y = as[i].x + MO2 - as[i + m].x; // < 4 MO as[i].x += as[i + m].x; // < 4 MO as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x; // < 2 MO as[i + m].x = (prod.x * y) % MO; // < MO } for (int i = i0 + (m >> 1); i < i0 + m; ++i) { const unsigned long long y = as[i].x + MO - as[i + m].x; // < 2 MO as[i].x += as[i + m].x; // < 2 MO as[i + m].x = (prod.x * y) % MO; // < MO } prod *= INV_FFT_RATIOS[__builtin_ctz(++h)]; } } if (m < n) { for (int i = 0; i < m; ++i) { const unsigned y = as[i].x + MO2 - as[i + m].x; // < 4 MO as[i].x += as[i + m].x; // < 4 MO as[i + m].x = y; // < 4 MO } } const Mint invN = Mint(n).inv(); for (int i = 0; i < n; ++i) { as[i] *= invN; } } void fft(vector &as) { fft(as.data(), as.size()); } void invFft(vector &as) { invFft(as.data(), as.size()); } vector convolve(vector as, vector bs) { if (as.empty() || bs.empty()) return {}; const int len = as.size() + bs.size() - 1; int n = 1; for (; n < len; n <<= 1) {} as.resize(n); fft(as); bs.resize(n); fft(bs); for (int i = 0; i < n; ++i) as[i] *= bs[i]; invFft(as); as.resize(len); return as; } vector square(vector as) { if (as.empty()) return {}; const int len = as.size() + as.size() - 1; int n = 1; for (; n < len; n <<= 1) {} as.resize(n); fft(as); for (int i = 0; i < n; ++i) as[i] *= as[i]; invFft(as); as.resize(len); return as; } //////////////////////////////////////////////////////////////////////////////// template struct ModInv { static constexpr unsigned M = M_; int k, k2, l; vector qs; vector> inv; ModInv() { k = cbrt(M); k2 = k * k; l = M / k; qs.assign(k2 + 1, 0); for (int q = k; q >= 1; --q) for (int p = 0; p <= q; ++p) qs[k2 * p / q] = q; for (int i = 1; i <= k2; ++i) if (!qs[i]) qs[i] = qs[i - 1]; inv.assign(l + 1, 0); inv[1] = 1; for (int i = 2; i <= l; ++i) inv[i] = -((M / i) * inv[M % i]); } ModInt operator()(const ModInt &a) const { const double r = static_cast(k2) * a.x / M; const int q0 = qs[r]; const ModInt b0 = a * q0; if (b0.x <= static_cast(l)) return inv[b0.x] * q0; const int q1 = qs[k2 - r]; return -inv[(-a * q1).x] * q1; } }; constexpr int L = 119 << 13; constexpr int M = 1 << 10; static_assert(L % M == 0); static_assert(L * M == MO-1); /* L^2 == L M == 0 (mod MO-1) (Li + Mj + k)^2 == 2Lik + M^2j^2 + 2Mjk + k^2 (mod MO-1) = (M^2j^2 - 2M binom(j,2)) + (2Lik - 2M binom(k,2) + k^2) + (2M binom(j+k,2)) */ int main() { const ModInv INV; for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) { Mint A; int N; scanf("%u%d", &A.x, &N); Mint ans = 0; const int N2 = N / L; const int N1 = N % L / M; const int N0 = N % M; // cerr<<"N2 = "<