#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i=0; i--) const i64 INF = 1001001001001001001; #include using Modint = atcoder::static_modint<10000'00007>; using namespace std; void testcase(){ int N; cin >> N; Modint P; { int p; cin >> p; P = p; } vector A(max(3,N+1)); A[1] = 1; for(int t=2; t<=N; t++) A[t] = A[t-1] * P + A[t-2]; vector B(max(4, N+2)); rep(i,N+1) B[i+1] = B[i] + A[i]; Modint ans = 0; rep(i,N) ans += (B[N] - B[i]) * A[i]; cout << ans.val() << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }