N, p = map(int, input().split()) MOD = 1000000007 a = [0] * N a[0] = 0 a[1] = 1 for i in range(2, N): a[i] = p * a[i-1] + a[i-2] revcum = [0] * N c = 0 for i, v in enumerate(a[::-1]): c += v revcum[i] = c ans = sum([i * j % MOD for i, j in zip(a, revcum[::-1])]) % MOD print(ans)