import sys INF = 1 << 60 MOD = 10**9 + 7 # 998244353 sys.setrecursionlimit(2147483647) input = lambda:sys.stdin.readline().rstrip() def resolve(): n, p = map(int, input().split()) if n == 1: print(0) n -= 1 dp = [0] * (n + 1) dp[1] = 1 for i in range(2, n + 1): dp[i] = p * dp[i - 1] + dp[i - 2] dp[i] %= MOD s = sum(dp) % MOD t = sum(v**2 for v in dp) % MOD ans = (s**2 + t) * pow(2, MOD - 2, MOD) % MOD print(ans) resolve()