#include using namespace std; using ll = long long; ll N, P, MOD = 1e9 + 7; vector A(2000005); void calc() { A[0] = 0; A[1] = 1; for (int i = 2; i < 2000005; i++) { A[i] = (P * A[i - 1] + A[i - 2]) % MOD; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; cin >> P; calc(); ll sum = 0; for (int i = 0; i < N; i++) { sum = (sum + A[i]) % MOD; } sum = (sum * sum) % MOD; ll sumA2 = 0; for (int i = 0; i < N; i++) { ll temp = (A[i] * A[i]) % MOD; sumA2 = (sumA2 + temp) % MOD; } sum = (sum - sumA2 + MOD) % MOD; sum = (sum * 500000004) % MOD; ll ans = (sum + sumA2) % MOD; cout << ans << '\n'; return 0; }