#include using namespace std; /*^ debug */ template string to_string(pair p); template string to_string(tuple p); template string to_string(tuple p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template string to_string(bitset v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast('0' + v[i]); } return res; } template string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template string to_string(pair p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template string to_string(tuple p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template string to_string(tuple p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif /* debug $*/ /*^ vector extensions */ template T concat(initializer_list lists) { T a; for (auto &l : lists) a.insert(a.end(), l.begin(), l.end()); return a; } template struct _Matrix_type { typedef vector::type> type; }; template struct _Matrix_type { typedef T type; }; template struct _Matrix { static auto build(size_t s) { return vector(s); } template static auto build(size_t f, Args... args) { return vector::type>(f, _Matrix::build(args...)); } }; template auto buildMatrix(Args... args) { return _Matrix::build(args...); } /* vector extensions $*/ /*^ generic definitions */ template struct _RecurFun : F { _RecurFun(F&& f) : F(forward(f)) {} template decltype(auto) operator()(Args&&... args) const { return F::operator()(*this, forward(args)...); } }; template decltype(auto) RecurFun(F&& f) { return _RecurFun { forward(f) }; } /* generic definitions $*/ /* https://codeforces.com/contest/1466/submission/102810637 */ template T inverse(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template class Modular { public: using Type = typename decay::type; constexpr Modular() : value() {} template Modular(const U& x) { value = normalize(x); } template static Type normalize(const U& x) { Type v; if (-mod() <= x && x < mod()) v = static_cast(x); else v = static_cast(x % mod()); if (v < 0) v += mod(); return v; } const Type& operator()() const { return value; } template explicit operator U() const { return static_cast(value); } constexpr static Type mod() { return T::value; } Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; } template Modular& operator+=(const U& other) { return *this += Modular(other); } template Modular& operator-=(const U& other) { return *this -= Modular(other); } Modular& operator++() { return *this += 1; } Modular& operator--() { return *this -= 1; } Modular operator++(int) { Modular result(*this); *this += 1; return result; } Modular operator--(int) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template typename enable_if::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { #ifdef _WIN32 uint64_t x = static_cast(value) * static_cast(rhs.value); uint32_t xh = static_cast(x >> 32), xl = static_cast(x), d, m; asm( "divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (mod()) ); value = m; #else value = normalize(static_cast(value) * static_cast(rhs.value)); #endif return *this; } template typename enable_if::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) { long long q = static_cast(static_cast(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template typename enable_if::Type>::value, Modular>::type& operator*=(const Modular& rhs) { value = normalize(value * rhs.value); return *this; } Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); } friend const Type& abs(const Modular& x) { return x.value; } template friend bool operator==(const Modular& lhs, const Modular& rhs); template friend bool operator<(const Modular& lhs, const Modular& rhs); template friend V& operator>>(V& stream, Modular& number); private: Type value; }; template bool operator==(const Modular& lhs, const Modular& rhs) { return lhs.value == rhs.value; } template bool operator==(const Modular& lhs, U rhs) { return lhs == Modular(rhs); } template bool operator==(U lhs, const Modular& rhs) { return Modular(lhs) == rhs; } template bool operator!=(const Modular& lhs, const Modular& rhs) { return !(lhs == rhs); } template bool operator!=(const Modular& lhs, U rhs) { return !(lhs == rhs); } template bool operator!=(U lhs, const Modular& rhs) { return !(lhs == rhs); } template bool operator<(const Modular& lhs, const Modular& rhs) { return lhs.value < rhs.value; } template Modular operator+(const Modular& lhs, const Modular& rhs) { return Modular(lhs) += rhs; } template Modular operator+(const Modular& lhs, U rhs) { return Modular(lhs) += rhs; } template Modular operator+(U lhs, const Modular& rhs) { return Modular(lhs) += rhs; } template Modular operator-(const Modular& lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } template Modular operator-(const Modular& lhs, U rhs) { return Modular(lhs) -= rhs; } template Modular operator-(U lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } template Modular operator*(const Modular& lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } template Modular operator*(const Modular& lhs, U rhs) { return Modular(lhs) *= rhs; } template Modular operator*(U lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } template Modular operator/(const Modular& lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } template Modular operator/(const Modular& lhs, U rhs) { return Modular(lhs) /= rhs; } template Modular operator/(U lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } template Modular power(const Modular& a, const U& b) { assert(b >= 0); Modular x = a, res = 1; U p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res; } template bool IsZero(const Modular& number) { return number() == 0; } template string to_string(const Modular& number) { return to_string(number()); } // U == std::ostream? but done this way because of fastoutput template U& operator<<(U& stream, const Modular& number) { return stream << number(); } // U == std::istream? but done this way because of fastinput template U& operator>>(U& stream, Modular& number) { typename common_type::Type, long long>::type x; stream >> x; number.value = Modular::normalize(x); return stream; } /* using ModType = int; struct VarMod { static ModType value; }; ModType VarMod::value; ModType& md = VarMod::value; using Mint = Modular; */ constexpr int md = (int) 1e9 + 7; using Mint = Modular::type, md>>; /*vector fact(1, 1); vector inv_fact(1, 1); Mint C(int n, int k) { if (k < 0 || k > n) { return 0; } while ((int) fact.size() < n + 1) { fact.push_back(fact.back() * (int) fact.size()); inv_fact.push_back(1 / fact.back()); } return fact[n] * inv_fact[k] * inv_fact[n - k]; }*/ int main() { ios::sync_with_stdio(false); int N, P; { cin >> N >> P; } vector A(N); { A[0] = 0; A[1] = 1; for (int i = 2; i < N; ++i) A[i] = P * A[i - 1] + A[i - 2]; } vector pa(N + 1); { for (int i = 0; i < N; ++i) pa[i + 1] = pa[i] + A[i]; } Mint res = 0; { for (int i = 0; i < N; ++i) res += pa[i + 1] * A[i]; } cout << res << endl; }