#include #define REP(i, n) for (int i = 0; i < n; i++) #define ALL(obj) obj.begin(), obj.end() #define fcout cout << setprecision(10) template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int iINF = 1e9; const long long llINF = 1e18; const int MOD = 1e9 + 7; using namespace std; int main() { int N; long long p; cin >> N >> p; vector a(N + 1, 0), b; a[0] = 0; a[1] = 0; a[2] = 1; for (int i = 3; i < N + 1; i++) { a[i] = (p * a[i - 1]) % MOD + a[i - 2] % MOD; a[i] %= MOD; } b = a; REP(i, N) { b[i + 1] += b[i]; b[i + 1] %= MOD; } long long ans = 0; for (int i = 1; i < N + 1; i++) { ans += a[i] * (b[i]); ans %= MOD; } cout << ans << endl; return 0; }