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

const int MAXN = 3e3+5;
const ll mod = 1e9+7;

int n, m;
ll dp[2][MAXN][MAXN];

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>m;		
	dp[1][0][0] = 1;
	for(int i=1; i<=n; ++i) {
		for(int j=0; j<=min(m, n); ++j) {
			dp[0][i][j] = dp[0][i-1][j] * (m - j) % mod;
			dp[1][i][j] = (dp[0][i-1][j] + dp[1][i-1][j]) * j % mod;
			if(j > 0) dp[0][i][j] = (dp[0][i][j] + dp[1][i-1][j-1] * (m - j + 1) % mod) % mod;
		}
	}
	ll ans = 0;
	for(int i=0; i<=n; ++i) {
		ans += dp[1][n][i];
		ans %= mod;
	}
	cout<<ans<<'\n';
}