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 <bits/stdc++.h>
using namespace std;
#define inf 1000000000000000007
using ll = long long;

int n, m;
ll mod, wynik, dp[2][10000007], pom;

int main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m >> mod;
    for (int i=1; i<=m; i++)
        dp[0][i] = m-i+1;
    for (int poz=2; poz<=n; poz++)
    {
        dp[1][1] = 0;
        for (int i=1; i<=m; i++)
        {
            dp[1][1] += (ll)(m-i+1)*dp[0][i];
            dp[1][1] %= mod;
        }
        pom = dp[0][1];
        for (int i=2; i<=m; i++)
        {
            ll odejmij = (ll)(m-i+1)*dp[0][m-i+2];
            odejmij %= mod;
            dp[1][i] = dp[1][i-1] + mod + mod - pom - odejmij;
            dp[1][i] %= mod;
            pom += dp[0][i] + mod - dp[0][m-i+2];
            pom %= mod;
        }

        for (int i=1; i<=m; i++)
        {
            dp[0][i] = dp[1][i];
            dp[1][i] = 0;
        }
    }
    for (int i=1; i<=m; i++)
    {
        wynik += dp[0][i];
        wynik %= mod;
    }
    cout << wynik << "\n";

    return 0;
}