MOD = 10 ** 9 + 7 N, p = map(int, input().split()) if N == 1: print(0) exit() 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)