#include #define rep(i,n) for(int i=(0);i<(n);i++) using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; // pow ll pow(ll a, ll b){ if(b == 0) return 1; if(b & 1) { return a * pow(a, b - 1); } else { ll d = pow(a, b / 2); return d * d; } } ll powmod(ll a, ll b){ if(b == 0) return 1; if(b & 1) { return a * powmod(a, b - 1) % MOD; } else { ll d = powmod(a, b / 2) % MOD; return d * d % MOD; } } ll sub(ll a, ll b){ return a + MOD - b; } ll inv(ll a){ return powmod(a, MOD - 2); } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll n, p; cin >> n >> p; if(n == 1){ cout << 0 << endl; return 0; } ll y = 1; ll z = 0; ll sm = y + z; rep(i, n - 2){ ll x = p * y + z; x %= MOD; sm += x; sm %= MOD; z = y; y = x; } y = 1; z = 0; ll dg = y * y + z * z; rep(i, n - 2){ ll x = p * y + z; x %= MOD; dg += x * x; dg %= MOD; z = y; y = x; } ll ans = dg; ll ot = sm * sm; ot %= MOD; ot += MOD - dg; ot %= MOD; ot *= inv(2); ans += ot; ans %= MOD; cout << ans << endl; }