#include #include using namespace std; const long long MOD = 1000000007; int main(){ 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]) % MOD; } long long sum = 0; long long sqsum = 0; for (int i = 0; i < N; i++){ sum += a[i]; sum %= MOD; sqsum += a[i] * a[i]; sqsum %= MOD; } cout << (sum * sum + sqsum) % MOD * 500000004 % MOD << endl; }