#include using namespace std; template struct Modular { using M = Modular; unsigned v; Modular(long long a = 0) : v((a %= Mod) < 0 ? a + Mod : a) {} M operator-() const { return M() -= *this; } M& operator+=(M r) { if ((v += r.v) >= Mod) v -= Mod; return *this; } M& operator-=(M r) { if ((v += Mod - r.v) >= Mod) v -= Mod; return *this; } M& operator*=(M r) { v = (uint64_t)v * r.v % Mod; return *this; } M& operator/=(M r) { return *this *= power(r, Mod - 2); } friend M operator+(M l, M r) { return l += r; } friend M operator-(M l, M r) { return l -= r; } friend M operator*(M l, M r) { return l *= r; } friend M operator/(M l, M r) { return l /= r; } friend bool operator==(M l, M r) { return l.v == r.v; } }; constexpr long long mod = 1e9 + 7; using Mint = Modular; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, p; cin >> n >> p; vector a(n); for (int i = 0; i < n; ++i) { if (i < 2) { a[i] = i; } else { a[i] = p * a[i - 1] + a[i - 2]; } } Mint res, sum; for (int i = 0; i < n; ++i) { sum += a[i]; res += sum * a[i]; } cout << res.v << '\n'; }