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
#include <algorithm>
#include <cstdio>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define INT(x) int x; scanf("%d", &x)

typedef long long LL;

const int mod = 1000000007;
int r[3001][3001][2];

int main() {
	INT(n);
	INT(m);
	r[0][0][1] = 1;
	REP(i,n) {
		int mm = min(m, i);
		REP(j,mm+1) {
			r[i + 1][j][0] = (r[i + 1][j][0] + LL(m - j) * r[i][j][0]) % mod;
			r[i + 1][j][1] = (r[i + 1][j][1] + LL(j) * (r[i][j][0] + r[i][j][1])) % mod;
			r[i + 1][j + 1][0] = (r[i + 1][j + 1][0] + LL(m - j) * r[i][j][1]) % mod;
		}
	}
	int s = 0;
	int mm = min(m, n);
	REP(j,mm+1) s = (s + r[n][j][1]) % mod;
	printf("%d\n", s);
}