import sys input=lambda: sys.stdin.readline().rstrip() n,p=map(int,input().split()) mod=10**9+7 if n==1: print(0) elif n==2: print(1) else: A=[0,1] B=[0,1] for _ in range(n-2): A.append((p*A[-1]+A[-2])%mod) B.append((A[-1]**2)%mod) a,b=0,0 for i in range(n): a=(a+A[i])%mod b=(b+B[i])%mod print(((a**2+b)*pow(2,mod-2,mod))%mod)