#include using namespace std; using ll=long long; using pii=pair; #define all(a) a.begin(),a.end() #define pb push_back #define sz(a) ((int)a.size()) const int N=100005; const int mod=1000000007; int add(int x, int y){x+=y; if(x>=mod) x-=mod; return x;} int sub(int x, int y){x-=y; if(x<0) x+=mod; return x;} int mul(int x, int y){return ((ll)x)*y%mod;} int Pow(int x, ll y=mod-2){int res=1; for(; y; x=mul(x,x),y>>=1) if(y&1) res=mul(res,x); return res;} int k,n,dp[N][7]; // 0 // 1 // 2, same // 2, different // 3, all same // 3, two same // 3, all different signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); cin >> k >> n; dp[0][0]=max(n,0); dp[0][1]=dp[0][2]=dp[0][4]=max(n-1,0); dp[0][3]=dp[0][5]=max(n-2,0); dp[0][6]=max(n-3,0); for(int i=1; i<=k; ++i){ dp[i][0]=mul(mul(n,dp[i-1][1]),mul(dp[i-1][1],dp[i-1][1])); dp[i][1]=add(mul(dp[i-1][1],mul(dp[i-1][2],dp[i-1][2])),mul(mul(n-1,dp[i-1][1]),mul(dp[i-1][3],dp[i-1][3]))); dp[i][2]=add(mul(dp[i-1][4],mul(dp[i-1][2],dp[i-1][2])),mul(mul(n-1,dp[i-1][5]),mul(dp[i-1][3],dp[i-1][3]))); dp[i][4]=add(mul(dp[i-1][4],mul(dp[i-1][4],dp[i-1][4])),mul(mul(n-1,dp[i-1][5]),mul(dp[i-1][5],dp[i-1][5]))); if(n>=2){ dp[i][3]=add(mul(mul(2,dp[i-1][5]),mul(dp[i-1][2],dp[i-1][3])),mul(mul(n-2,dp[i-1][6]),mul(dp[i-1][3],dp[i-1][3]))); dp[i][5]=add(add(mul(dp[i-1][4],mul(dp[i-1][5],dp[i-1][5])),mul(dp[i-1][5],mul(dp[i-1][5],dp[i-1][5]))),mul(mul(n-2,dp[i-1][5]),mul(dp[i-1][6],dp[i-1][6]))); } if(n>=3){ dp[i][6]=add(mul(mul(3,dp[i-1][6]),mul(dp[i-1][5],dp[i-1][5])),mul(mul(n-3,dp[i-1][6]),mul(dp[i-1][6],dp[i-1][6]))); } } cout << dp[k][0] << "\n"; }