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
#include<bits/stdc++.h>
using namespace std;
using lld = long long;

const int MAXN = 3000;
const lld mod = 1e9+7;

lld ok[MAXN+1][MAXN+1];
lld bad[MAXN+1][MAXN+1];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	lld m;
	cin >> n >> m;

	bad[1][1] = m;
	for(int i=2;i<=n;i++)
		bad[1][i] = (bad[1][i-1]*(m-1)) % mod;
	for(int i=1;i<=n;i++)
		ok[1][i] = (bad[1][i-1] + ok[1][i-1]) % mod;

	for(int i=2;i<=min(n,(int) m);i++) {
		for(int j=1;j<=n;j++) {
			bad[i][j] += ok[i-1][j-1] * (m-i+1);
			bad[i][j] %= mod;
			bad[i][j] += bad[i][j-1] * (m-i);
			bad[i][j] %= mod;
		}

		for(int j=1;j<=n;j++) {
			ok[i][j] += ok[i][j-1] * i;
			ok[i][j] %= mod;
			ok[i][j] += bad[i][j-1] * i;
			ok[i][j] %= mod;
		}
	}

	lld res = 0;
	for(int i=1;i<=n;i++)
		res += ok[i][n];
	res %= mod;

	cout << res << "\n";

	return 0;
}