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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<cstdio>
#include<algorithm>
#include<vector>
#define S 10000007

using namespace std;
typedef long long ll;

ll dp[2][S];
ll dp2[2][S];

ll wyn[2];

int main(void){
    int n,m;
    ll mod;
    scanf("%d %d %lld",&n,&m,&mod);

    wyn[0] = 1;

    for(int i = 1; i <= n;i++){
        ll x = 0;
        int now = i%2;
        int op = (i+1)%2;
        for(int j = 1; j <= m;j++){
            dp[now][j] = (ll)j * wyn[op] - (ll)j * dp2[op][j+1] - x;
            dp[now][j] += dp[now][j-1];
            dp[now][j] %= mod;
            if(dp[now][j] < 0)
                dp[now][j] += mod;
            x += dp[op][j];
            //dp2[i][j] = (m-j+1) * wyn[i-1] - (m-j+1) * dp[i-1][j-1] - dp4[i-1][j+1];
        }
        x = 0;
        for(int j = m; j >= 1;j--){
            dp2[now][j] = (ll)(m-j+1) * wyn[op] - (ll)(m-j+1) * dp[op][j-1] - x;
            dp2[now][j] += dp2[now][j+1];
            dp2[now][j] %= mod;
            if(dp2[now][j] < 0)
                dp2[now][j] += mod;
            x += dp2[op][j];

        }
        wyn[now] = dp[now][m];
    }

    printf("%lld",wyn[n%2]);

    return 0;
}