n,p = map(int,input().split()) mod = 10**9+7 a = [0]*(n) a[0] = 0 a[1] = 1 for i in range(2,n): a[i] = p*a[i-1]+a[i-2] a[i] %= mod rui = [0]*(n+1) for i in range(n): rui[i+1] = rui[i]+a[i] rui[i+1] %= mod ans = 0 for i in range(n): ans += a[i]*(rui[i+1]-rui[0]+mod)%mod ans %= mod print(ans)