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