#include using namespace std; typedef long long int ll; typedef vector vl; typedef pair PP; #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define all(v) v.begin(), v.end() #define inputv(v, n) \ vl v; \ rep(i, n) { \ ll x; \ cin >> x; \ v.push_back(x); \ } bool chmin(ll& a, ll& b) { if (b < a) { a = b; return 1; } return 0; } bool chmax(ll& a, ll& b) { if (b > a) { a = b; return 1; } return 0; } const ll INF = 999999999999999; const ll MOD = 1000000007; const ll MAX_N = 500010; ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans; #include template class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x% Modulus) {} constexpr u64& value() noexcept { return a; } constexpr const u64& value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint& operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint& operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint& operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint& operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; using mint = modint; int main() { cin >> n >> p; vector A,B; mint ans = 0,wa=0; A.push_back(0); A.push_back(1); rep(i, n) { A.push_back(A[i] + A[i + 1] * p); } rep(i, n) { wa += A[i]; } wa = wa * wa; wa=wa; rep(i, n) { wa += A[i]*A[i]; } wa = wa / 2; cout << wa.a << endl; }