MOD = 10 ** 9 + 7 n, p = map(int, input().split()) if n == 1: print(0) exit() dp = [0, 1] for _ in range(n - 2): x = p * dp[-1] + dp[-2] dp.append(x % MOD) tot = sum(dp) ans = tot * tot + sum(d ** 2 for d in dp) ans %= MOD print(ans * pow(2, MOD - 2, MOD) % MOD)