#include using namespace std; using i64 = long long; const int N = 1e5 + 10, P = 1e9 + 7; int k, n; int qpow(int b, int k) { int ret = 1; while(k > 0) { if(k & 1) ret = (i64)ret * b % P; b = (i64)b * b % P; k >>= 1; } return ret; } i64 ch(i64 x) { return max(x, 0ll); } int sav[N][7], vis[N][7]; int solve(int k, int type) { if(k == 0) { if(type == 0) return n; else if(type == 1 || type == 3 || type == 6) return ch(n - 1); else if(type == 2 || type == 5) return ch(n - 2); else if(type == 4) return ch(n - 3); } if(!vis[k][type]) { vis[k][type] = true; if(type == 0) sav[k][type] = (i64)n * qpow(solve(k - 1, 1), 3) % P; else if(type == 1) sav[k][type] = solve(k - 1, 1) * (qpow(solve(k - 1, 3), 2) + ch(n - 1ll) * qpow(solve(k - 1, 2), 2) % P) % P; else if(type == 2) sav[k][type] = ((i64)solve(k - 1, 5) * solve(k - 1, 2) % P * solve(k - 1, 3) * 2 + ch(n - 2ll) * solve(k - 1, 4) % P * qpow(solve(k - 1, 2), 2)) % P; else if(type == 3) sav[k][type] = ((i64)solve(k - 1, 6) * qpow(solve(k - 1, 3), 2) + ch(n - 1ll) * solve(k - 1, 5) % P * qpow(solve(k - 1, 2), 2)) % P; else if(type == 4) sav[k][type] = (3ll * solve(k - 1, 4) % P * qpow(solve(k - 1, 5), 2) + ch(n - 3ll) * qpow(solve(k - 1, 4), 3)) % P; else if(type == 5) sav[k][type] = (solve(k - 1, 6) * (i64)qpow(solve(k - 1, 5), 2) + qpow(solve(k - 1, 5), 3) + ch(n - 2ll) * solve(k - 1, 5) % P * qpow(solve(k - 1, 4), 2)) % P; else if(type == 6) sav[k][type] = (qpow(solve(k - 1, 6), 3) + ch(n - 1ll) * qpow(solve(k - 1, 5), 3)) % P; } return sav[k][type]; } /* 0: uncolored 1: 1 side colored 2: 2 side colored with different colors 3: 2 side colored with the same color 4: 3 side colored with 3 colors 5: 3 side colored with 2 colors 6: 3 side colored with 1 color */ int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> k >> n; cout << solve(k, 0); }