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
50
51
52
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define st first
#define nd second
#define pb push_back
#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define all(a) a.begin(),a.end()
const int mxN = 3e3+7;
const int MOD = 1e9+7;
ll mno[mxN];
ll sil[mxN];
ll pot(ll pod,ll wyk){
	ll res = 1;
	while(wyk){
		if(wyk%2 == 1) res *= pod;
		pod *= pod;
		wyk /= 2;
		pod %= MOD;
		res %= MOD;
	}
	return res;
}

ll npok(ll n,ll k){
	if(k == 0) return 1;
	ll res = sil[n];
	res = (res*pot(sil[k],MOD-2))%MOD;
	res = (res*pot(sil[n-k],MOD-2))%MOD;
	return res;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll n,k; cin >> n >> k;
	mno[1] = k;
	rep(i,2,n) mno[i] = (mno[i-1]*(k-i+1))%MOD;
	sil[0] = 1;
	rep(i,1,n) sil[i] = (sil[i-1]*i)%MOD;
	ll res = 0;
	ll i = 1;
	while(2*i <= n && i <= k){
		ll ress = mno[i];
		ress = (ress*npok(n-i-1,i-1))%MOD;
		ress = (ress*pot(k,n-2*i))%MOD;
		res = (res+ress)%MOD;
		i++;
	}
	cout << res << "\n";
}