#include #include #include #include #include #include #include using namespace std; using ll = long long; constexpr int P = 1000000007; int main() { int n, p; cin >> n >> p; vector a(n); a[0] = 0; a[1] = 1; ll s = a[0] + a[1]; for (int i = 2; i < n; i++) { s += a[i] = ((ll)p * a[i - 1] + a[i - 2]) % P; } s %= P; ll r = 0; for (int i = 0; i < n; i++) { r += a[i] * s % P; s -= a[i]; if (s < 0) s += P; } r %= P; cout << r << endl; return 0; }