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
#include<bits/stdc++.h>

using namespace std;

const int M=1e9+7;

long long T[3005][3005];

//long long smki[]


int main(){
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
	long long n,m;cin>>n>>m;
	long long W=m;
	for(int i=1;i<n;i++){
		T[i][i+1]=W*i;
		T[i][i+1]=(T[i][i+1]+M)%M;
		W*=m-i;W=(W+M)%M;
	}
	
	for(long long i=n-2;i>0;i--){
		long long R=0;long long K=T[i][i+1];
		for(long long j=i+2;j<=n;j++){
			R*=i;R=(R+M)%M;K*=i;K=(K+M)%M;
			R+=T[i+1][j-1]*i;R=(R+M)%M;
			
			T[i][j]=T[i][j-1]*(m-i);T[i][j]=(T[i][j]+M)%M;
			
			T[i][j]+=R;T[i][j]=(T[i][j]+M)%M;
			
			T[i][j]+=K;T[i][j]=(T[i][j]+M)%M;
		}
	}
	
	/*for(int i=1;i<n;i++){
		for(int j=1;j<=n;j++)cout<<i<<" "<<j<<" "<<T[i][j]<<"\n";
	}*/
	
	
	cout<<T[1][n];
	
return 0;
}