#include #include #include #include #include #include #include "testlib.h" using namespace std; using namespace atcoder; using mint = modint998244353; using i32 = int32_t; using u64 = uint64_t; #define REP(i, s, t) for (i32 i = (s); i < (t); ++i) #define PER(i, s, t) for (i32 i = (t)-1; i >= (s); --i) vector fact, ifact, inv; const i32 m = 100000; void init(void) { fact.resize(m + 1, 1); ifact.resize(m + 1, 1); inv.resize(m + 1, 1); REP(i, 1, m + 1) fact.at(i) = i * fact.at(i - 1); ifact.at(m) = fact.at(m).inv(); PER(i, 1, m) { inv.at(i + 1) = ifact.at(i + 1) * fact.at(i); ifact.at(i) = ifact.at(i + 1) * (i + 1); } } int main(int argc, char **argv) { cin.tie(nullptr); ios::sync_with_stdio(false); registerValidation(argc, argv); init(); const i32 k = inf.readInt(1, m); inf.readSpace(); const i32 q = inf.readInt(1, m); inf.readEoln(); vector dp(m + 1); dp[0] = 1; mint sum = 1; REP(i, 1, m + 1) { if (i > k) sum -= dp[i - k - 1]; dp[i] = sum * inv[i]; sum += dp[i]; } auto ep = convolution(dp, dp); REP(i, 0, q) { const i32 n = inf.readInt(1, m); inf.readSpace(); const i32 l = inf.readInt(1, n); inf.readSpace(); const i32 r = inf.readInt(l, n); inf.readEoln(); const u64 q = numeric_limits::max() - (u64)998244352 * 998244352; const u64 sub = q / 998244353 * 998244353; u64 sum = 0; REP(i, l, r + 1) { sum += (u64)dp[i - 1].val() * dp[n - i].val(); if (sum >= q) { sum -= sub; } } mint ans = sum; ans *= fact[n - 1]; cout << ans.val() << "\n"; } inf.readEof(); return 0; }