#include using namespace std; const int M = 1000000007; long long modexp(int x, long long e, int m) { long long ans = 1, p = x % m; while (e > 0) { if (e % 2 != 0) ans = (ans * p) % m; p = (p * p) % m; e >>= 1; } return ans; } int main() { long long n, k; cin >> n >> k; k %= M; cout << ((modexp(((k + 1) * (k + 2) / 2 - 1) % M, n, M) - modexp(k * (k + 1) / 2 % M, n, M)) % M + M) % M << endl; }