#pragma GCC optimize("O3") #include using namespace std; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i struct ModInt { #if __cplusplus >= 201402L #define MDCONST constexpr #else #define MDCONST #endif using lint = long long; MDCONST static int mod() { return md; } int val_; int val() const noexcept { return val_; } MDCONST ModInt() : val_(0) {} MDCONST ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; } MDCONST ModInt(lint v) { _setval(v % md + md); } MDCONST explicit operator bool() const { return val_ != 0; } MDCONST ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val_ + x.val_); } MDCONST ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val_ * x.val_ % md); } MDCONST friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { return os << x.val_; } }; using mint = ModInt<1000000007>; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int K, N; cin >> K >> N; if (K == 0) { cout << N << '\n'; return 0; } const int M = max(0, N - 2), U = max(0, N - 3); mint g = N - 1, h = N - 1, i = M, j = N - 1, k = M, l = U; REP(_, K - 1) { mint gnxt = g * (h * h + i * i * (N - 1)); mint hnxt = j * h * h + k * i * i * (N - 1); mint inxt = k * i * h * 2 + l * i * i * M; mint jnxt = j * j * j + k * k * k * (N - 1); mint knxt = (j + k) * k * k + k * l * l * M; mint lnxt = l * (k * k * 3 + l * l * U); g = gnxt; h = hnxt; i = inxt; j = jnxt; k = knxt; l = lnxt; } cout << (g * g * g * N).val() << '\n'; }