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