def main(): from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum from heapq import heapify, heappop, heappush from bisect import bisect_left, bisect_right from copy import deepcopy import copy import random from collections import deque,Counter,defaultdict from itertools import permutations,combinations from decimal import Decimal,ROUND_HALF_UP #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP) from functools import lru_cache, reduce #@lru_cache(maxsize=None) from operator import add,sub,mul,xor,and_,or_,itemgetter INF = 10**18 mod1 = 10**9+7 mod2 = 998244353 #DecimalならPython #再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!! ''' やば 主客転倒 Kがでかすぎる 式で処理できないと無理 漸化式立てて行列累乗とかあり得る 真部分集合は全く同じものは含まないらしい 足してからかけてもよい ''' N,K = map(int, input().split()) tmp = (K+3)*K%mod1*pow(2,mod1-2,mod1)%mod1 up = pow(tmp,N,mod1) tmp = (K+1)*K%mod1*pow(2,mod1-2,mod1)%mod1 dwn = pow(tmp,N,mod1) print((up-dwn)%mod1) if __name__ == '__main__': main()