N,K = map(int,input().split()) MOD = 1000000007 ans = pow(K*(K + 3)//2,N,MOD) - pow(K*(K + 1)//2,N,MOD) + MOD ans %= MOD print(ans)