#include #define rep(i, n) for(ll i=0; i<(n); ++i) #define rep1(i,n) for(ll i=1; i<=(n); ++i) #define repi(i,a,b) for(ll i=a; i<=(b); ++i) #define rrep(i,n) for(ll i=(n-1); i>=0; --i) #define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr) #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define pb push_back #define mp make_pair #define to_s to_string #define sz(v) (int)v.size() #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) #define print(x) cout<<(x)<<'\n' #define debug(x) cout << #x << ": " << (x) << '\n' using namespace std; using ll = long long; using Edge = pair; using Graph = vector>; typedef pair P; struct aaa{aaa(){ cin.tie(0); ios::sync_with_stdio(0); cout< par; vector rank; vector num; UnionFind(int N) : par(N),rank(N),num(N) { for(int i = 0; i < N; i++){ par[i] = i; rank[i] = 0; num[i] = 1; } } void init(int N){ for(int i = 0; i < N; i++){ par[i] = i; rank[i] = 0; num[i] = 1; } } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } int ranker(int x){ x = root(x); return rank[x]; } int number(int x){ x = root(x); return num[x]; } void unite(int x, int y){ int rx = root(x); int ry = root(y); if ( rx == ry ) return; if ( rank[rx] < rank[ry] ){ par[rx] = ry; num[ry] += num[rx]; num[rx] = 0; } else { par[ry] = rx; num[rx] += num[ry]; num[ry] = 0; if ( rank[rx] == rank[ry] ) rank[rx]++; } } bool same(int x, int y){ return root(x) == root(y); } }; int main(){ ll N, p; cin >> N >> p; ll dp[N+5]; dp[1] = 0, dp[2] = 1; for(int i = 3; i <= N; ++i) dp[i] = (p*dp[i-1] + dp[i-2]) % MOD; ll sum[N+5]; sum[1] = dp[1]; for(int i = 2; i <= N; ++i) sum[i] = (sum[i-1] + dp[i]) % MOD; ll ans = 0; rep1(i,N) ans += (dp[i]*sum[i]) % MOD, ans %= MOD; print(ans); return 0; }