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

#define ll long long

using namespace std;

int n,m,g;
ll p;

ll add(ll a, ll b) {
  return ((a%p)+(b%p))%p;
}

ll mul(ll a, ll b) {
  return ((a%p)*(b%p))%p;
}

ll pow(ll a, ll b) {
  if(b > 1) {
    ll x = pow(a,b/2);
    x = x*x;
    x %= p;
    if((b%2)==1) {
      x *= a;
      x %= p;
    }
    return x;
  } else if(b == 1) {
    return a%p;
  } else {
    return 1%p;
  }
}

ll div(ll a, ll b) {
  return mul(a,pow(b,p-2));
}

vector<vector<ll>> coeff = { {},
  { 1, 0 },
  { 2, 2, 1, 0 },
  { 33, 50, 45, 29, 12, 0 },
  { 32, 65, 83, 77, 53, 30, 12, 0 },
  { 36740, 93345, 152940, 183414, 169260, 126905, 76660, 42588, 18000, 0 },
  { 353448, 1077043, 2145275, 3146154, 3600894, 3362139, 2605515, 1741388, 994708, 593568, 279360, 0 } };
vector<ll> leading = { 1, 1, 1, 11, 8, 7348, 58908 };
vector<ll> den = { 0, 2, 6, 180, 360, 907200, 19958400 };

ll solveFormula() {
  ll res = leading[n] % p;
  vector<ll>& c = coeff[n];
  for(auto it = c.begin();it != c.end();it++) {
    res = mul(res, m);
    res = add(res, *it);
  }
  res = div(res, den[n]%p);
  return res;
}

bool overlap(pair<int, int>& a, pair<int, int>& b) {
  return ((a.second >= b.first) && (b.second >= a.first));
}

ll** allocMat() {
  ll** mat = new ll*[g];
  for(int i=0;i<g;i++) {
    mat[i] = new ll[g];
    for(int j=0;j<g;j++) {
      mat[i][j] = 0;
    }
  }
  return mat;
}

void mulMat(ll** a, ll** b, ll** c) {
  for(int i=0;i<g;i++) {
    for(int j=0;j<g;j++) {
      ll z = 0;
      for(int k=0;k<g;k++) {
        z += (a[i][k]*b[k][j])%p;
        z %= p;
      }
      c[i][j] = z;
    }
  }
}

ll** powMat(ll** a, ll u) {
  ll highest = 1LL << 62;
  while((highest > 0) && ((highest & u) == 0)) highest >>= 1;
  ll** cur = allocMat();
  for(int i=0;i<g;i++) {
    cur[i][i] = 1;
  }
  ll** tmp = allocMat();
  while(highest > 0) {
    mulMat(cur, cur, tmp);
    if((highest & u) > 0) {
      mulMat(tmp, a, cur);
    } else {
      ll** t = tmp;
      tmp = cur;
      cur = t;
    }
    highest >>= 1;
  }
  return cur;
}

ll solveMatrix() {
  vector<pair<int, int>> states;
  for(int i=0;i<m;i++) {
    for(int j=i;j<m;j++) {
      states.push_back(pair<int, int>(i,j));
    }
  }
  g = states.size();
  ll** a = allocMat();
  for(int i=0;i<g;i++) {
    for(int j=i;j<g;j++) {
      if(i == j) {
        a[i][i] = 1;
      } else {
        ll d = (overlap(states[i], states[j])?1:0)%p;
        a[i][j] = d;
        a[j][i] = d;
      }
    }
  }

  ll** q = powMat(a,n);
  ll res = 0;
  for(int i=0;i<g;i++) {
    res += q[m-1][i];
    res %= p;
  }
  return res;
}

int main(int argc, char** argv) {
  scanf("%d%d%lld",&n,&m,&p);
  
  ll res;
  if((n <= 6) && (m >= 10)) {
    res = solveFormula();
  } else {
    res = solveMatrix();
  }
  printf("%lld\n", res);
  return 0;
}