n, p = map(int, input().split()) MOD = 10**9 + 7 if n == 1: print(0) exit() a = [0] * n a[1] = 1 for i in range(2, n): a[i] = p * a[i - 1] + a[i - 2] a[i] %= MOD diag = 0 for i in range(n): diag += a[i] * a[i] diag %= MOD sum_a = sum(a) % MOD ans = ((sum_a * sum_a) - diag) * pow(2, MOD - 2, MOD) + diag print(ans % MOD)