N, K = map(int, input().split()) P = 10 ** 9 + 7 ans = pow(K * (K + 3) // 2, N, P) - pow(K * (K + 1) // 2, N, P) print(ans % P)