#include constexpr int kN = int(2E6 + 10), kMod = 1000000007; typedef long long int ll; ll a[kN], s[kN]; int main() { int n, p; ll ans = 0; scanf("%d%d", &n, &p); a[1] = 0, a[2] = 1; for (int i = 3; i <= n; i++) a[i] = (a[i - 1] * p + a[i - 2]) % kMod; s[1] = 0; for (int i = 2; i <= n; i++) s[i] = (s[i - 1] + a[i]) % kMod; for (int i = 1; i <= n; i++) ans += a[i] * s[i] % kMod; printf("%lld\n", ans % kMod); }