mod=10**9+7 n,p=map(int,input().split()) if n==1: print(0) exit() ans=1 acum=1 fibs=[0,1,0] for i in range(n-2): fibs[2]=p*fibs[1]+fibs[0] fibs[2]%=mod acum+=fibs[2] ans+=fibs[2]*(acum) ans%=mod fibs[0],fibs[1]=fibs[1],fibs[2] print(ans)