#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(i64 i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; #include #include #include namespace nachia{ // ax + by = gcd(a,b) // return ( x, - ) std::pair ExtGcd(long long a, long long b){ long long x = 1, y = 0; while(b){ long long u = a / b; std::swap(a-=b*u, b); std::swap(x-=y*u, y); } return std::make_pair(x, a); } } // namespace nachia namespace nachia{ class DynamicModSupplier{ using u64 = unsigned long long; using Int = unsigned int; private: u64 imod; Int mod; // atcoder library u64 reduce2(u64 z) const noexcept { // atcoder library #ifdef _MSC_VER u64 x; _umul128(z, im, &x); #else using u128 = unsigned __int128; u64 x = (u64)(((u128)(z)*imod) >> 64); #endif return z - x * mod; } public: DynamicModSupplier(unsigned int MOD = 998244353) : mod(MOD) { assert(2 <= MOD); assert(MOD < (1u << 31)); imod = (u64)(-1) / mod + 1; } Int reduce(u64 z) const noexcept { Int v = reduce2(z); if(mod <= v) v += mod; return v; } Int add(Int a, Int b) const { a += b; if(a >= mod){ a -= mod; } return a; } Int sub(Int a, Int b) const { a -= b; if(a >= mod){ a += mod; } return a; } Int mul(Int a, Int b) const { return reduce((u64)a * b); } Int muladd(Int a, Int b, Int c) const { return reduce((u64)a * b + c); } Int inv(Int a) const { Int v = ExtGcd(a, mod).first; return (v < mod) ? v : (v + mod); } Int pow(Int a, u64 i) const { Int r = a, ans = 1; while(i){ if(i & 1) ans = mul(ans, r); i /= 2; r = mul(r, r); } return ans; } Int getMod() const { return mod; } }; } // namespace nachia namespace nachia{ template struct GarnerMod{ using Int = unsigned int; using IntLong = unsigned long long; std::vector mods; std::vector dynmods; std::vector> table_coeff; std::vector table_coeffinv; void precalc(std::vector new_mods){ mods = std::move(new_mods); dynmods.resize(mods.size()); for(size_t i=0; i(nmods, 1)); for(int j=0; j& x) const { int nmods = mods.size(); std::vector table_const(nmods); FinishType res = 0; FinishType res_coeff = 1; for(int j=0; j calc(std::vector> x) const { int n = x[0].size(), m = x.size(); std::vector res(n); std::vector buf(m); for(int i=0; i mods; for(Int p=2; p*p<=m; p++) if(m%p == 0){ Int pe = 1; int e = 0; do{ e += 1; pe *= p; m /= p; } while(m%p == 0); mpf.emplace_back(p, pe, e); mods.push_back(pe); } if(m != 1){ mpf.emplace_back(m, m, 1); mods.push_back(m); } mg.precalc(mods); } Int comb(Int2 n, Int2 r) const { std::vector rems; for(auto& f : mpf) rems.push_back(f.comb(n, r)); return mg.calc(rems); } Int operator()(Int2 n, Int2 r) const { return comb(n,r); } private: struct CombinationModPrimePower{ DynamicModSupplier dyn; std::vector F; Int p; int e; CombinationModPrimePower(Int xp, Int xm, int xe) : dyn(xm), F(xm+1), p(xp), e(xe) { F[0] = 1; for(Int s=0; s fact(Int2 n) const { Int2 ze = 0; Int2 qe = 0; Int f = 1; int k = 0; while(n){ Int x = n % dyn.getMod(); f = dyn.mul(f, F[x]); n /= p; ze += n; if(++k >= e) qe += n; } if(qe % 2 == 1 && !(p == 2 && e >= 3)) f = dyn.getMod() - f; return { ze, f }; } Int comb(Int2 n, Int2 r) const { if(r < 0 || n < r) return 0; auto [ne, nf] = fact(n); auto [re, rf] = fact(r); auto [se, sf] = fact(n-r); Int2 ze = ne - re - se; if(e <= ze) return 0; Int f = dyn.mul(nf, dyn.inv(dyn.mul(rf, sf))); for(int i=0; i mpf; GarnerMod mg; }; } // namespace nachia auto comb = nachia::CombinationAnyMod(998243353); void testcase(){ i64 T, Tau; cin >> T >> Tau; rep(t,Tau){ i64 N, M; cin >> M >> N; if(t != T-1) cout << comb(N,M) << "\n"; else cout << "-1\n"; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }