#include using namespace std; #define MOD 1000000007 long long memo[2000001]; long long memo2[2000002]; int main(){ int n,p;cin>>n>>p; memo[1] = 0; memo[2] = 1; for(int i = 3; n >= i; i++){ memo[i] = (p*memo[i-1]+memo[i-2])%MOD; } memo2[0] = 0; memo2[1] = memo[1]; for(int i = 2; n >= i; i++){ memo2[i] = (memo2[i-1]+memo[i])%MOD; } long long ans = 0; for(int i = 1; n >= i; i++){ ans = (ans+memo[i]*(memo2[i]-memo2[0]))%MOD; } cout << ans << endl; }