1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
long long prevs[10000003], curr[10000003];
long long p, n, m;
int main() {
    cin >> n >> m >> p;
    for (int i = 1; i <= m; i++) {
        curr[i] = (curr[i-1] + i) % p;
        prevs[i] = curr[i];
    }
    for (int i = 2; i <= n; i++) {
        long long integral_prefix = 0;
        for (int j = 1; j <= m; j++) {
            curr[j] = (curr[j-1] + (p + j*(p + prevs[m] - prevs[m-j]) - integral_prefix)) % p;
            integral_prefix += prevs[j];
            integral_prefix %= p;
        }
        for (int j = 1; j <= m; j++) {
            prevs[j] = curr[j];
        }
    }
    cout << curr[m] << endl;
}