import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N,p = MI() mod = 10**9+7 A = [0,0,1] for _ in range(N-2): A.append((p*A[-1]+A[-2]) % mod) x = sum(A) % mod y = sum(a**2 for a in A) % mod ans = (x**2+y)*(mod+1)//2 print(ans % mod)