#include using namespace std; int main() { const int64_t mod = 1000000007; int n, p; cin >> n >> p; vector a(n); if (n > 1) a.at(1) = 1; for (int i = 2; i < n; i++) { a.at(i) = p * a.at(i - 1) + a.at(i - 2); a.at(i) %= mod; } int64_t s = 0, s_diag = 0; for (int i = 0; i < n; i++) { s += a.at(i); s %= mod; s_diag += a.at(i) * a.at(i); s_diag %= mod; } int64_t ans = (s * s + s_diag) % mod; if (ans % 2) ans += mod; ans /= 2; cout << ans << endl; }