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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "bits/stdc++.h"

using namespace std;
#define PB push_back
#define LL long long
#define int LL
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define VI vector<int>
#define PII pair<int,int>
#define LD long double

template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream &os, vector<T> V){
  os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
} 
template<class T, unsigned long X> ostream &operator<<(ostream &os, array<T,X> V){
  os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
} 

template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}


#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif
#ifdef LOCAL
const int MX = 10;
const int K = 12;
#else
const int MX = 1 << 10;
const int K = 12;
#endif
int n,k,mod;
array<array<int, MX>, MX> binom;
array<array<int, MX>, K> dyn_eat_single;
array<array<int, MX>, K> dyn_eat_both;
array<array<int, MX>, K> pref_dyn_eat_single;
array<array<int, MX>, K> pref_dyn_eat_both;

void tup(int & a){
  if(a >= mod){
    a -= mod;
  }
}

void compute_binom(){
  binom[0][0] = 1;
  FOR(i, 1, n){
    binom[i][0] = 1;
    FOR(j, 1, i){
      binom[i][j] = binom[i - 1][j] + binom[i - 1][j - 1];
      tup(binom[i][j]);
    }
  }
}

int compute_dyn(){
  for(PII p: {PII(0,0)}){
    int a,b;
    tie(a,b) = p;
    dyn_eat_single[a][b] = dyn_eat_both[a][b] = 1;
    pref_dyn_eat_both[a][b] = pref_dyn_eat_single[a][b] = 1;
  }

  FOR(r, 1, K - 1){
    pref_dyn_eat_single[r][0]    = 1;
    pref_dyn_eat_both[r][0]    = 1;
    FOR(d, 1, n){
      int & res_single      = dyn_eat_single[r][d];
      int & res_both        = dyn_eat_both[r][d];
      int & pref_res_single = pref_dyn_eat_single[r][d];
      int & pref_res_both   = pref_dyn_eat_both[r][d];

      FOR(a, 0, d - 1){
        //a x b
        int b = d - a - 1;
        int mul = binom[d - 1][a] * binom[d - 1 - a][b] % mod;
        int tmp_single = 0;
        int tmp_both = 0;
        //single  
        //sum by a,b such max(a,b + 1) = r single[a][left] * single[b][right]  
        //a == r
        tmp_single += dyn_eat_single[r][a] * pref_dyn_eat_both[r - 1][b];
        tmp_single %= mod;
        //b + 1 == r && a < r
        tmp_single += pref_dyn_eat_single[r - 1][a] * dyn_eat_both[r - 1][b];
        tmp_single %= mod;
        //both
        //sum by a,b such max(a,b, min(a,b) + 1) = r both[a][left] * both[b][right]
        //a == r -> b <= r - 1
        tmp_both += dyn_eat_both[r][a] * pref_dyn_eat_both[r - 1][b];
        tmp_both %= mod;
        //b == r && a <= r - 1
        tmp_both += pref_dyn_eat_both[r - 1][a] * dyn_eat_both[r][b];
        tmp_both %= mod;
        //b == r - 1 && a == r - 1
        tmp_both += dyn_eat_both[r - 1][a] * dyn_eat_both[r - 1][b];
        tmp_both %= mod;

        res_single += tmp_single * mul;
        res_single %= mod;
        res_both += tmp_both * mul;
        res_both %= mod;
      }
      pref_res_single       = pref_dyn_eat_single[r - 1][d] + res_single;
      pref_res_both         = pref_dyn_eat_both[r - 1][d] + res_both;
      tup(pref_res_single);
      tup(pref_res_both);
    }
    debug(dyn_eat_both[r]);
  }
  int res = 0;
  FOR(a, 0, n - 1){
    int b = n - a - 1;
    int mul = binom[n - 1][a] * binom[n - 1 - a][b] % mod;
    int tmp = 0;
    tmp += dyn_eat_single[k][a] * pref_dyn_eat_single[k][b];
    //debug(tmp, k, a, b, dyn_eat_single[k][a], pref_dyn_eat_single[k][b]);
    tmp %= mod;
    tmp += pref_dyn_eat_single[k - 1][a] * dyn_eat_single[k][b];
    tmp %= mod;
    res += tmp * mul;
    res %= mod;
  }
  return res;
}

int get_ans(int n, int k){
  if(k >= K){
    return 0;
  }
  int res = 0;
  FOR(a, 0, n - 1){
    int b = n - a - 1;
    int mul = binom[n - 1][a] * binom[n - 1 - a][b] % mod;
    int tmp = 0;
    tmp += dyn_eat_single[k][a] * pref_dyn_eat_single[k][b];
    //debug(tmp, k, a, b, dyn_eat_single[k][a], pref_dyn_eat_single[k][b]);
    tmp %= mod;
    tmp += pref_dyn_eat_single[k - 1][a] * dyn_eat_single[k][b];
    tmp %= mod;
    res += tmp * mul;
    res %= mod;
  }
  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin>>n>>k>>mod;
  if(k >= K){
    cout<<0<<"\n";
    return 0;
  }
  compute_binom();
  cout<<compute_dyn()<<"\n";
}