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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1007
typedef long long ll;
ll m, n, p;
ll res[MAXN][MAXN], full_res;
bool have[MAXN][MAXN], have2[MAXN][MAXN];

ll least[MAXN][MAXN];

inline ll mmod(const ll &k){
  ll cp = k;
  while (cp < 0) cp += p;
  return cp % p;
}

ll between(int len, int a, int b);

ll ss(int len, int a, int south);
ll ss_cum(int len, int a, int south);
ll all(int len);

ll calc(int len, int a){
  if (have[len][a])
    return res[len][a];

  have[len][a] = true;

  ll my_all = all(len-1);
  ll north = ss_cum(len-1, a+1, 0);
  res[len][a] = mmod(my_all - north);
  return res[len][a];
}

ll at_least(int len, int a){
  // num colorings of length len, which last one starts at 0 and has at least a
  // elements
  if (have2[len][a])
    return least[len][a];
  
  //printf("at least len %d\n", len);

  have2[len][a] = true;
  least[len][a] = calc(len, a);
  if (a != m){
    least[len][a] += at_least(len, a+1);
    least[len][a] = mmod(least[len][a]);
  }

  //printf("least len %d a=%d %d\n", len, a, least[len][a]);
  return least[len][a];
}

bool have3[MAXN][MAXN];
ll super[MAXN][MAXN];
ll superset(int len, int a){
  // number of supersets of [a-1, a]
  if (have3[len][a])
    return super[len][a];

  have3[len][a] = true;

  ll my_all = all(len);
  ll north = 0ll;
  north = ss_cum(len, a, 0);
  ll my_south = ss_cum(len, a-1, 1);
  super[len][a] = mmod(my_all - north - my_south);
  return super[len][a];

}

bool have4[MAXN][MAXN][2];
ll halfsup[MAXN][MAXN][2];

ll ss(int len, int a, int south){
  if (have4[len][a][south])
    return halfsup[len][a][south];

  have4[len][a][south] = true;
  
  if (south){
    for(int i = 1; i <=a; i++){
      halfsup[len][a][south] += between(len, i, a);
      halfsup[len][a][south] = mmod(halfsup[len][a][south]);
    }
  } else {
    for(int i = a; i <=m; i++){
      halfsup[len][a][south] += between(len, a, i);
      halfsup[len][a][south] = mmod(halfsup[len][a][south]);
    }
  }
  return halfsup[len][a][south];
}

bool have6[MAXN][MAXN][2];
ll halfsup_cum[MAXN][MAXN][2];

ll ss_cum(int len, int a, int south){
  if (have6[len][a][south])
    return halfsup_cum[len][a][south];

  have6[len][a][south] = true;
  if (south){
    for(int i=a; i>=1; i--){
      halfsup_cum[len][a][south] += ss(len, i, south);
      halfsup_cum[len][a][south] = mmod(halfsup_cum[len][a][south]);
    }
  } else {
    for(int i = a; i<=m; i++){
      halfsup_cum[len][a][south] += ss(len, i, south);
      halfsup_cum[len][a][south] = mmod(halfsup_cum[len][a][south]);
    }
  }
  return halfsup_cum[len][a][south];
}

bool have5[MAXN];
ll all_mem[MAXN];
ll all(int len){
  if (have5[len])
    return all_mem[len];
  have5[len] = true;
  for(int i = 1; i<=m;i++){
    all_mem[len] += ss(len, i, 0);
    all_mem[len] = mmod(all_mem[len]);
  }
  return all_mem[len];
}

ll between(int len, int a, int b){
  // how many ending with a, b, 1-based
  if (len == 0){
    if (a == 1 && b == m) return 1;
    else return 0;
  }
  if (a == 1){
    return calc(len, b);
  }
  ll ress = mmod(mmod(calc(len, b) + superset(len - 1, a)) - calc(len, a-1));
  //printf("between %d %d, len %d, res %d\n", a, b, len, res);
  //printf("partial %d %d %d\n", calc(len, b), superset(len-1, a), calc(len,a-1));
  return ress;
}


int main(){
  scanf("%lld%lld%lld", &n,&m,&p);

  for(int i = 0; i <= m; i++){
    have[0][i] = true;
    have2[0][i] = true;
  }
  for(int i = 0; i<=n; i++){
    have2[i][0] = true;  // 0 of length at most 0
  }
  res[0][m] = 1ll;
  least[0][m] = 1ll;
  printf("%lld\n", calc(n+1, m));
}