N,P = map(int,input().split()) dp = [0,0,1]+[0]*(N-2) mod = 10**9+7 for i in range(3,N+1): dp[i] = P*dp[i-1]+dp[i-2] dp[i] %= mod alsm = sum(dp) ans = 0 for i in range(1,N+1): ans += dp[i]*alsm ans %= mod alsm -= dp[i] alsm %= mod print(ans)