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
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <cstdio>
#include <vector>

int dp[1001][15][2][2];
int npok[1001][1001];
int pow2[1001];

int gogo(int n, int k, int left, int right, int P) {
  if (n == 0) {
    return k == 0;
  } else if (n == 1) {
    if (k == 0) {
      return right || left;
    }
    else if (k == 1) {
      return !right && !left;
    }
    else {
      return 0;
    }
  }
  else {
    if (dp[n][k][left][right] != -1) return dp[n][k][left][right];

    if (k == 0) {
      int ret = 0;
      if (left == 0 && right == 0) {
        return dp[n][k][left][right] = 0;
      }
      else if (left == 1 && right == 0) {
        return dp[n][k][left][right] = 1;
      }
      else if (left == 0 && right == 1) {
        return dp[n][k][left][right] = 1;
      }
      else {
        return dp[n][k][left][right] = pow2[n - 1];
      }
    }

    unsigned long long ret = 0;

    if (left == 0 && right == 0) {
      // WSTAW NA PIERWSZEJ POZYCJI
      ret += gogo(n - 1, k - 1, 1, 0, P);
      for (int i = 1; i < n - 1; ++i) {
        // WSTAW na pozycji I
        unsigned long long ret_local = 0;
        for (int k1 = 0; k1 < k - 1; k1++) ret_local += ((unsigned long long)(gogo(i, k1, 0, 1, P))) * gogo(n - i - 1, k - 1, 1, 0, P);
        for (int k1 = 0; k1 < k - 1; k1++) ret_local += ((unsigned long long)(gogo(i, k - 1, 0, 1, P))) * gogo(n - i - 1, k1, 1, 0, P);
        ret_local += ((long long)(gogo(i, k - 1, 0, 1, P))) * gogo(n - i - 1, k - 1, 1, 0, P);
        ret += ((ret_local % P) * npok[n - 1][i]) % P;
      }
      // WSTAW NA OSTATNIEJ POZYCJI
      ret += gogo(n - 1, k - 1, 0, 1, P);
    }
    else if (left == 1 && right == 0) {

      // WSTAW NA PIERWSZEJ POZYCJI
      ret += gogo(n - 1, k, 1, 0, P);
      for (int i = 1; i < n - 1; ++i) {
        // WSTAW na pozycji I
        unsigned long long ret_local = 0;
        for (int k1 = 0; k1 < k - 1; k1++) {
          ret_local += ((unsigned long long)(gogo(i, k1, 1, 1, P))) * gogo(n - i - 1, k, 1, 0, P);
        }
        for (int k1 = 0; k1 < k - 1; k1++) {
          ret_local += ((unsigned long long)(gogo(i, k - 1, 1, 1, P))) * gogo(n - i - 1, k1, 1, 0, P);
        }
        ret_local += ((unsigned long long)(gogo(i, k - 1, 1, 1, P))) * gogo(n - i - 1, k, 1, 0, P);
        ret_local += ((unsigned long long)(gogo(i, k - 1, 1, 1, P))) * gogo(n - i - 1, k - 1, 1, 0, P);
        ret += ((ret_local % P) * npok[n - 1][i]) % P;
      }
      // WSTAW NA OSTATNIEJ POZYCJI
      ret += gogo(n - 1, k - 1, 1, 1, P);

    }
    else if (left == 0 && right == 1) {

      // WSTAW NA PIERWSZEJ POZYCJI
      ret += gogo(n - 1, k - 1, 1, 1, P);
      //if (n == 3) printf("++ %d\n", ret);
      for (int i = 1; i < n - 1; ++i) {
        // WSTAW na pozycji I
        unsigned long long ret_local = 0;
        for (int k1 = 0; k1 < k - 1; k1++) {
          ret_local += ((unsigned long long)(gogo(i, k1, 0, 1, P))) * gogo(n - i - 1, k - 1, 1, 1, P);
        }
        for (int k1 = 0; k1 < k - 1; k1++) {
          ret_local += ((unsigned long long)(gogo(i, k, 0, 1, P))) * gogo(n - i - 1, k1, 1, 1, P);
        }
        ret_local += ((unsigned long long)(gogo(i, k, 0, 1, P))) * gogo(n - i - 1, k - 1, 1, 1, P);
        ret_local += ((unsigned long long)(gogo(i, k - 1, 0, 1, P))) * gogo(n - i - 1, k - 1, 1, 1, P);
        ret += ((ret_local % P) * npok[n - 1][i]) % P;
      }
      // WSTAW NA OSTATNIEJ POZYCJI
      ret += gogo(n - 1, k, 0, 1, P);
    }
    else if (left == 1 && right == 1) {
      
      // WSTAW NA PIERWSZEJ POZYCJI
      ret += gogo(n - 1, k, 1, 1, P);
      for (int i = 1; i < n - 1; ++i) {
        // WSTAW na pozycji I
        unsigned long long ret_local = 0;
        for (int k1 = 0; k1 < k - 1; k1++) {
          ret_local += ((unsigned long long)(gogo(i, k1, 1, 1, P))) * gogo(n - i - 1, k, 1, 1, P);
        }
        for (int k1 = 0; k1 < k - 1; k1++) {
          ret_local += ((unsigned long long)(gogo(i, k, 1, 1, P))) * gogo(n - i - 1, k1, 1, 1, P);
        }
        ret_local += ((unsigned long long)(gogo(i, k, 1, 1, P))) * gogo(n - i - 1, k - 1, 1, 1, P);
        ret_local += ((unsigned long long)(gogo(i, k - 1, 1, 1, P))) * gogo(n - i - 1, k, 1, 1, P);
        ret_local += ((unsigned long long)(gogo(i, k - 1, 1, 1, P))) * gogo(n - i - 1, k - 1, 1, 1, P);
        ret += ((ret_local % P) * npok[n - 1][i]) % P;
      }
      // WSTAW NA OSTATNIEJ POZYCJI
      ret += gogo(n - 1, k, 1, 1, P);
    }

     
    return dp[n][k][left][right] = ret % P;
  }
}

int pr[13];
int cnt[13];

void perm(int k, int n) {
  if (k == n) {
    std::vector<int> curr;
    for (int i = 0; i < n; ++i) {
      curr.push_back(pr[i]);
    }
    int steps = 0;
    while (curr.size() > 1) {
      ++steps;
      std::vector<int> next;
      for (int j = 0; j < curr.size(); ++j) {
        if ((j == 0 || curr[j] > curr[j - 1]) && (j + 1 == curr.size() || curr[j] > curr[j + 1])) {
          next.push_back(curr[j]);
        }
      }
      curr = next;
    }
    ++cnt[steps];
    return;
  }

  for (int i = 0; i < n; ++i) {
    if (pr[i] == -1) {
      pr[i] = k;
      perm(k + 1, n);
      pr[i] = -1;
    }
  }
}

int main() {
  int n, k, p;
  scanf("%d %d %d", &n, &k, &p);
  /*
  for (int i = 0; i < n; ++i) {
    pr[i] = -1;
  }
  perm(0, n);
  for (int i = 0; i < n; ++i) printf("[%d] = %d\n", i, cnt[i]);
  */
  
  npok[0][0] = npok[1][0] = npok[1][1] = 1;
  pow2[0] = 1;
  pow2[1] = 2;
  for (int i = 2; i <= n; ++i) {
    pow2[i] = 2 * pow2[i - 1];
    if (pow2[i] >= p) pow2[i] -= p;
    npok[i][0] = npok[i][i] = 1;
    for (int j = 1; j < i; ++j) {
      npok[i][j] = npok[i - 1][j] + npok[i - 1][j - 1];
      if (npok[i][j] >= p) npok[i][j] -= p;
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j < 15; ++j) {
      dp[i][j][0][0] = dp[i][j][0][1] = dp[i][j][1][0] = dp[i][j][1][1] = -1;
    }
  }
  printf("%d\n", k < 12 ? gogo(n, k, 0, 0, p) : 0);
  return 0;
}