MOD = 10 ** 9 + 7 N, p = map(int, input().split()) A = [0] * max(3,(N + 1)) A[2] = 1 suma = 1 ans = 1 for i in range(3, N + 1): A[i] = p * A[i - 1] + A[i - 2] A[i] %= MOD suma += A[i] ans += A[i] * suma ans %= MOD print(ans)