#include using namespace std; #define ALL(obj) (obj).begin(),(obj).end() #define RALL(obj) (obj).rbegin(),(obj).rend() #define REP(i, n) for(int i=0;i<(n);++i) #define RANGE(i, a, b) for(int i=(a);i<(b);++i) #define RREP(i, n) for(int i=(n)-1;i>= 0;--i) #define endl '\n' #define pb push_back #define eb emplace_back #define pque priority_queue #define umap unordered_map #define uset unordered_set #define BIG 2000000000 #define VERYBIG 1000000000000000ll #define PI 3.141592653589793238 #define dcout cout< inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;} template inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;} template inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;} template inline T nHr(T a,T b){return nCr(a+b-1,b);} template inline bool chmax(T& a,T b){if(a inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} typedef pair INTP; typedef vector INTV; struct edge2 {int to, cost;}; struct edge3 {int from, to, cost;}; int powmod(int a, int p, int m){ int res = 1, tmp = a; while(p != 0){ if(p % 2) res = (res*tmp) % m; tmp = (tmp*tmp) % m; p /= 2; } return res; } int fact[2000], invfact[2000]; int invmod(int a, int p){ // a^-1 mod p int b = p, u = 1, v = 0; while(b){ int t = a / b; a -= t*b; swap(a, b); u -= t*v; swap(u, v); } u %= p; if(u < 0) u += p; return u; } void factmod(int m){ fact[0] = invfact[0] = 1; for(int i = 1; i < 2000; i++){ fact[i] = (fact[i-1] * i) % m; invfact[i] = (invfact[i-1] * invmod(i, m)) % m; } } int nCrmod(int n, int r, int m){ return (((fact[n] * invfact[r]) % m) * invfact[n-r]) % m; } signed main(void){ cin.tie(0); ios::sync_with_stdio(false); int N, K; cin >> N >> K; if(N < K){ cout << 0 << endl; return 0; } factmod(MOD); int ans = 0; for(int emp = 1; emp < K; emp++){ int num = nCrmod(K, emp, MOD) * powmod(K-emp, N, MOD); num %= MOD; if(emp & 1){ ans += num; ans %= MOD; } else { ans += MOD; ans -= num; ans %= MOD; } } ans = powmod(K, N, MOD) + MOD - ans; ans %= MOD; cout << ans << endl; }