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

using namespace std;



const int MAXN = 3010;
const long long MOD = 1000000007;
long long fwd[MAXN];
long long loop[MAXN];
long long paths[MAXN][MAXN];

int main() {
  int n;
  long long m;
  scanf("%d%lld", &n, &m);
  int states = min(n + 1, 2 * (int)m + 1);

  for (int i = 0; i < states; i++) {
    if (i % 2 == 0) {
      fwd[i] = m - (i / 2);
      loop[i] = (i / 2);
    } else {
      fwd[i] = (i / 2) + 1;
      loop[i] = m - (i / 2) - 1;
    }
  }

  paths[0][0] = 1;
  for (int v = 1; v < states; v++) {
    for (int k = 1; k <= n; k++) {
      paths[v][k] = paths[v - 1][k - 1] * fwd[v - 1] + paths[v][k - 1] * loop[v];
      paths[v][k] %= MOD;
    }
  }

  long long out = 0;
  for (int i = 0; i < states; i += 2) {
    out += paths[i][n];
  }
  out %= MOD;
  printf("%lld\n", out);
}