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
51
52
53
54
55
#include <bits/stdc++.h>
#define boost                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define int long long
#define st first
#define nd second
using namespace std;

const int N(1e6 + 13);

int n, q, m, prime, prefSum, myWays;
int **ways;

void initWays() {
  ways = new int *[n + 13];
  for (int i = 0; i <= n; i++) {
    ways[i] = new int[m + 13];
  }
}

int rangeSum(int n) { return n * (n + 1) / 2; }
int mod(int value) { return value % prime; }
int nMod(int value) { return value < 0 ? value + prime : value; }

int32_t main() {
  boost;
  cin >> n >> m >> prime;
  initWays();

  for (int i = 1; i <= m; i++) {
    ways[1][i] = rangeSum(i);
  }

  for (int i = 2; i <= n; i++) {
    prefSum = 0;

    for (int j = 1; j <= m; j++) {
      myWays = nMod(ways[i - 1][m] - ways[i - 1][m - j]);
      myWays = mod(j * myWays);
      myWays = mod(ways[i][j - 1] + myWays);
      myWays = nMod(myWays - prefSum);
      prefSum = mod(prefSum + ways[i - 1][j]);

      ways[i][j] = myWays;
    }
  }

  cout << ways[n][m] << endl;
  /*
    1592 6281 1000000007
  */
}