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
#include <cstdio>
#include <vector>

using namespace std;

vector<long long> sums, cum_sums, cum_cum_sums;

int main()
{
  int n, m, p;
  
  scanf("%d %d %d", &n, &m, &p);
  
  sums.resize(m + 1, 0);
  cum_sums.resize(m + 1, 0);
  cum_cum_sums.resize(m + 1, 0);
  
  long long S = 0;
  for (int i = 1; i <= m; i++)
  {
    sums[i] = i;
    S += sums[i];
    S %= p;
    cum_sums[i] = cum_sums[i - 1] + sums[i];
    cum_sums[i] %= p;
    cum_cum_sums[i] = cum_cum_sums[i - 1] + cum_sums[i];
    cum_cum_sums[i] %= p;
  }
  
  
  
  for (int k = 2; k <= n; k++)
  {
    for (int i = 1; i <= m; i++)
    {
      long long a = 1LL * S * i;
      a %= p;
      a -= cum_cum_sums[i - 1];
      if (a < 0) a += p;
      long long b = 1LL * i * cum_sums[m - i];
      b %= p;
      a -= b;
      if (a < 0) a += p;
      sums[i] = a;
    }
    
    S = 0;
    for (int i = 1; i <= m; i++)
    {
      cum_cum_sums[i] = 0;
      cum_sums[i] = 0;
      S += sums[i];
      S %= p;

      cum_sums[i] = cum_sums[i - 1] + sums[i];
      cum_sums[i] %= p;
      cum_cum_sums[i] = cum_cum_sums[i - 1] + cum_sums[i];
      cum_cum_sums[i] %= p;
    }
  }
    
  printf("%lld\n", S);
  return 0;
}