#include #include #include #include #include #include #include using namespace std; using ll = long long; constexpr int P = 1000000007; ll powmod(ll n, ll k) { ll r = 1, t = n % P; for (; k != 0; k /= 2) { if (k & 1) r = r * t % P; t = t * t % P; } return r; } ll modinv(ll n) { return powmod(n, P - 2); } int main() { int n; ll k; cin >> n >> k; ll t = k % P * ((k + 1) % P) % P * modinv(2) % P; t = powmod(t, n); ll s = ((k + 1) % P * ((k + 2) % P) % P * modinv(2) + P - 1) % P; s = powmod(s, n); s -= t; s %= P; if (s < 0) s += P; cout << s << endl; return 0; }