#include #ifdef _DEBUG #include "debug.hpp" #else #define debug(...) #endif #define REP(i, m, n) for(int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) using namespace std; using ll = long long; using ld = long double; using pll = pair; const int INF = 1<<30; const ll LINF = 1LL<<60; const ld PI = 3.1415926535897932; template inline bool chmax(T &a, const U &b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, const U &b) { if(a > b) { a = b; return true; } return false; } constexpr int MAX_N = 2010101; constexpr int MOD = 1e9+7; //constexpr int MOD = 998244353; struct mint { long long x; mint(long long x = 0) noexcept : x((x%MOD + MOD) % MOD) {} // basis mint operator-() const { return mint(-x); } mint& operator+=(const mint a) noexcept { if((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint a) noexcept { if((x += MOD-a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint a) noexcept { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const noexcept { return mint(*this) += a; } mint operator-(const mint a) const noexcept { return mint(*this) -= a; } mint operator*(const mint a) const noexcept { return mint(*this) *= a; } mint mpow(long long t) const noexcept { if(!t) return 1; mint a = mpow(t>>1); a *= a; if(t&1) a *= *this; return a; } // for prime MOD mint inv() const noexcept { return mpow(MOD-2); } mint& operator/=(const mint a) noexcept { return *this *= a.inv(); } mint operator/(const mint a) const noexcept { return mint(*this) /= a; } // comparison bool operator==(const mint &a) const noexcept { return this->x == a.x; } bool operator!=(const mint &a) const noexcept { return this->x != a.x; } // I/O stream friend istream& operator>>(istream& is, mint& a) noexcept { return is >> a.x; } friend ostream& operator<<(ostream& os, const mint& a) noexcept { return os << a.x; } }; // additional mint mpow(const long long &a, ll n) noexcept { return mint(a).mpow(n); } mint mpow(const mint &a, ll n) noexcept { return a.mpow(n); } struct combination { vector fact, ifact; combination(int n) : fact(n+1), ifact(n+1) { assert(n < MOD); fact[0] = 1; for(int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i; ifact[n] = fact[n].inv(); for(int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i; } mint operator()(int n, int k) { if(k < 0 || k > n) return 0; return fact[n]*ifact[k]*ifact[n-k]; } } com(MAX_N); signed main() { cin.tie(0); ios::sync_with_stdio(false); ll N, p; cin >> N >> p; vector a(N+10); a[1] = 0; a[2] = 1; REP(i, 3, N+1) a[i] = mint(p)*a[i-1]+a[i-2]; vector b(N+10, 0); REP(i, 1, N+1) b[i] = b[i-1]+a[i]; mint ans = 0; REP(i, 1, N+1) { ans += a[i]*b[i]; } cout << ans << '\n'; return 0; }