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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<vector>

#define mp make_pair
#define fi first
#define se second

using namespace std;

const int N = 1e7;
vector<long long>v[N];
vector<long long>super_pref[N];
long long prev_wrong;

int n, m;
long long mod;

long long good_per_one() {
  long long res = 0;
  for (int i = 1; i <= m; i++) {
    res += i;
    res %= mod;
  }
  return res;
}

void add_super_pref(int i) {
  long long pref = 0;
  for (int j = 0; j < m; j++) {
    pref += v[i][j];
    pref %= mod;
    super_pref[i].push_back(pref);
  }
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m >> mod;

  const long long single = good_per_one();
  for (int i = 0; i < m; i++) {
    v[0].push_back(i + 1);
    if (i > 0) {
      v[0].back() += v[0][i - 1];
      v[0].back() %= mod;
    }
  }
  long long all = single;

  for (int i = 1; i < n; i++) {
    add_super_pref(i - 1);
    long long curr_wrong = 0;
    for (int j = 0; j < m; j++) {
      long long bad = (prev_wrong * (j + 1)) % mod;
      if (m - j - 2 >= 0) {
        bad += (v[i - 1][m - j - 2] * (j + 1)) % mod;
        bad %= mod;
      }
      if (j > 0) {
        bad += super_pref[i - 1][j - 1];
        bad %= mod;
      }

      curr_wrong += bad;
      curr_wrong %= mod;
      v[i].push_back(((all * (j + 1) % mod) - bad + mod) % mod);
      if (j > 0) {
        v[i].back() += v[i][j - 1];
        v[i].back() %= mod;
      }
    }
    all *= single;
    all %= mod;
    prev_wrong = curr_wrong;
  }

  cout << v[n - 1].back() << endl;

  return 0;
}