MOD = 10 ** 9 + 7 INF = 10 ** 10 import sys sys.setrecursionlimit(100000000) dy = (-1,0,1,0) dx = (0,1,0,-1) def main(): n,p = map(int,input().split()) dp = [0] * n dp[1] = 1 cum = [0] * n cum[1] = 1 for i in range(2,n): dp[i] = (p * dp[i - 1] + dp[i - 2])%MOD cum[i] = (cum[i - 1] + dp[i])%MOD ans = 0 for i in range(n): ans += dp[i] * cum[i] ans %= MOD print(ans) if __name__ =='__main__': main()