#include using namespace std; typedef long long ll; typedef pair l_l; typedef pair i_i; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) const ll mod = 1000000007; ll a[2010000]; int main() { //cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); ll N, p; cin >> N >> p; a[1] = 0; a[2] = 1; for(int i = 3; i <= N; i++) { a[i] = p * a[i-1] + a[i-2]; a[i] %= mod; } ll asum = 0; ll asquaresum = 0; for(int i = 1; i <= N; i++) { asum += a[i]; asquaresum += a[i] * a[i]; asum %= mod; asquaresum %= mod; } ll ans = asum * asum % mod; ans += asquaresum; ans %= mod; ans *= 5e8 + 4; ans %= mod; cout << ans << endl; return 0; }