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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int32_t main(){
    ios::sync_with_stdio(false);
    ll m,n,p;
    cin >> m >> n >> p;
    vector<vector<ll> > up(m+1,vector<ll>(n));
    auto ab = up;

    for(ll a=0;a<n;a++) {
        up[1][a] = n-a;
        ab[1][a] = (n-a)*(ll)(a+1)%p;
    }
    
    for(int i=2;i<=m;i++) {
        vector<ll> upSum(n+1);
        upSum[n-1] = up[i-1][n-1];
        for(ll b=n-2,ct=2;b>=0;b--,ct++)
            upSum[b] = (upSum[b+1] + ct*up[i-1][b])%p;
        
        for(ll a=0;a<n;a++) 
            up[i][a] = ((n-a)*ab[i-1][a] + upSum[a+1])%p;
    

        ab[i][0] = up[i][0];
        for(ll a=1;a<n;a++)
            ab[i][a] = ((ab[i][a-1] - up[i][n-a] + up[i][a])%p+p)%p; //down[i][a-1] = up[i][n-a]
        
    }
    ll res = 0;
    for(int a=0;a<n;a++)
        res += up[m][a];
    cout<<res%p<<"\n";
}