#include #include #include #include #define rep(i,n) for(int i=0; i<(int)(n); i++) using namespace std; using LL = long long; using P = pair; const int Max_N = 1e9; const int Max_K = 1500; const int Max_M = 2e5; const LL mod = 998244353; LL modpow(LL x, LL n){ LL r = 1; while(n){ if(n & 1) r = r * x % mod; x = x * x % mod; n >>= 1; } return r; } LL dp[2000][2000][2]; LL sum1[2000][2000][2], sum2[4000][2000][2]; int main(){ int N, K, M; cin >> N >> K >> M; assert(1 <= N and N <= Max_N); assert(1 <= K and K <= Max_K); assert(1 <= M and M <= min(Max_M, K * (K+1) / 2)); vector

S(M); vector L(1), R(1); vector erase(M); rep(i,M){ int l, r; cin >> l >> r; assert(1 <= l and l <= r and r <= K); S[i] = P(l, r); } sort(S.begin(), S.end()); rep(i,M-1) assert(S[i] != S[i+1]); rep(i,M-1) if(S[i+1].second <= S[i].second) erase[i] = true; rep(i,M-1){ if(erase[i]) continue; for(int j=i+1; j= d) dp[j][r][p] += sum1[j-d][s][p^1]; dp[j][r][p] += sum2[j-r+Max_K][pre_r][p^1] - sum2[j-r+Max_K][s][p^1]; dp[j][r][p] %= mod; (sum1[j][r][p] += sum1[j][pre_r][p] + dp[j][r][p]) %= mod; (sum2[j-r+Max_K][r][p] += sum2[j-r+Max_K][pre_r][p] + dp[j][r][p]) %= mod; } } } vector power(K+1); rep(i,K+1) power[i] = modpow(i, N); LL res = 0, ans = 0; rep(j,K+1){ rep(k,K+1){ rep(p,2) (dp[j][k][p] *= power[K-j]) %= mod; } } rep(j,K+1){ rep(k,K+1) (res += dp[j][k][1] - dp[j][k][0]) %= mod; } ans = (modpow(K, N) - res) % mod; while(ans < 0) ans += mod; cout << ans << endl; return 0; }