from collections import * from itertools import * from functools import * from heapq import * import math,sys input = sys.stdin.readline mod = 10**9 + 7 N,K = map(int,input().split()) A = pow(K*(K+3)//2,N,mod) B = pow(K*(K+1)//2,N,mod) print((A-B)%mod)