#include #define REP(i, x, y) for (ll i = x; i <= y; i++) #define BIT(t) (1ll << t) #define PER(i, y, x) for (ll i = y; i >= x; i--) #define vll vector #define vvll vector> #define pll pair #define SIZE(v) ll(v.size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); using namespace std; typedef long long ll; // ios::sync_with_stdio(false); // cin.tie(nullptr); ll const MOD = 1000000007; ll mod_p(ll x, ll y) { x %= MOD; y %= MOD; return (x + y + MOD) % MOD; } ll mod_m(ll x, ll y) { x %= MOD; y %= MOD; return x * y%MOD; } ll mod_pow(ll x, ll t) { x %= MOD; if (t == 0) { return 1; } else { ll v = mod_pow(x, t / 2); if (t % 2 == 0) { return v * v % MOD; } else { return v * v%MOD * x %MOD; } } } ll mod_inv(ll x) { return mod_pow(x, MOD - 2); } int main(){ ll n, p; cin >> n >> p; ll sm = 0; ll dd = 0; vll a(n+1); a[1] = 0;a[2] = 1; REP(i,3,n){ a[i] = mod_p(mod_m(a[i-1], p), a[i-2]) ; } REP(i,1,n){ sm += a[i]; dd += mod_m(a[i], a[i]); } sm = mod_m(sm, sm); cout << mod_m(mod_inv(2), mod_p(sm, dd)) << endl; }