1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdio>

const int max_n = 10000100;
int n, m, p;

int F[2][max_n];

int main() {
    scanf ("%d %d %d", &n, &m, &p);

    for (int i = 0; i <= m; ++i) {
        F[0][i] = 0;
    }
    F[0][m] = 1;

    int side = 0;
    for (int i = 1; i <= n; ++i) {
        side = 1 - side;
        F[side][0] = 0;
        long long pref_sum = F[1 - side][0];
        for (int j = 1; j <= m; ++j) {
            int p1 = ((long long)(j) * F[1 - side][m]) % p;
            int p2 = ((long long)(j) * F[1 - side][m - j]) % p;
            int ret = F[side][j - 1];
            ret = (ret + p1) % p;
            ret = (ret + p - p2) % p;
            ret = (ret + p - pref_sum) % p;
            F[side][j] = ret;
            pref_sum = (pref_sum + F[1 - side][j]) % p;
        }
    }
    printf("%d\n", F[side][m]);
    
    return 0;
}