1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;

const int N = 3005 + 5;
const long long mod = 1e9 + 7;

int n, m;
long long dp[N][N][2];

int main() {
	scanf("%d%d", &n, &m);
	dp[1][1][0] = m;
	for (int i = 2; i <= n; i++) {
		for (int j = 1; j <= min(i, m); j++) {
			dp[i][j][1] = j * (dp[i - 1][j][0] + dp[i - 1][j][1]) % mod;
			dp[i][j][0] = ((long long) (m - j) * dp[i - 1][j][0] + dp[i - 1][j - 1][1] * (m - j + 1)) % mod;
		}
	}
	long long res = 0;
	for (int i = 1; i <= n; i++) {
		res = (res + dp[n][i][1]) % mod;
	}
	printf("%lld\n", res);
}