#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } template struct ModInt { static constexpr int M = M_; int x; constexpr ModInt() : x(0) {} constexpr ModInt(long long x_) : x(x_ % M) { if (x < 0) x += M; } ModInt &operator+=(const ModInt &a) { x += a.x; if (x >= M) x -= M; return *this; } ModInt &operator-=(const ModInt &a) { x -= a.x; if (x < 0) x += M; return *this; } ModInt &operator*=(const ModInt &a) { x = static_cast((static_cast(x) * a.x) % M); return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } ModInt operator-() const { return ModInt(-x); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt x2 = x, xe = 1; for (; e; e >>= 1) { if (e & 1) xe *= x2; x2 *= x2; } return xe; } ModInt inv() const { int a = x, b = M, y = 1, z = 0, t; for (; ; ) { t = a / b; a -= t * b; if (a == 0) { assert(b == 1 || b == -1); return ModInt(b * z); } y -= t * z; t = b / a; b -= t * a; if (b == 0) { assert(a == 1 || a == -1); return ModInt(a * y); } z -= t * y; } } friend ModInt operator+(long long a, const ModInt &b) { return (ModInt(a) += b); } friend ModInt operator-(long long a, const ModInt &b) { return (ModInt(a) -= b); } friend ModInt operator*(long long a, const ModInt &b) { return (ModInt(a) *= b); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; constexpr int MO = 1000000007; using Mint = ModInt; constexpr int LIM = 1'000'010; Mint inv[LIM], fac[LIM], invFac[LIM]; void prepare() { inv[1] = 1; for (int i = 2; i < LIM; ++i) { inv[i] = -(Mint::M / i) * inv[Mint::M % i]; } fac[0] = invFac[0] = 1; for (int i = 1; i < LIM; ++i) { fac[i] = fac[i - 1] * i; invFac[i] = invFac[i - 1] * inv[i]; } } Mint binom(Int n, Int k) { if (0 <= k && k <= n) { assert(n < LIM); return fac[n] * invFac[k] * invFac[n - k]; } else { return 0; } } Mint getInv(int d) { return (d < 0) ? -inv[-d] : inv[d]; } int N; Int M; vector A; int Q; vector X, Y; vector AA; Mint ans; vector goal; vector sums; Mint calc(int u, int v) { const int a = A[u], b = A[v], c = A[N]; const Mint aa = AA[u], bb = AA[v], cc = AA[N]; Mint ret = -(aa * (b - c) + bb * (c - a) + cc * (a - b)); ret *= getInv(a - b); ret *= getInv(b - c); ret *= getInv(c - a); return ret; } void add(int u, int v) { if (v == N) { ans += sums[u]; goal[u] = 1; } else { const Mint here = calc(u, v); if (goal[v]) { ans += here; } sums[v] += here; } } int main() { prepare(); for (; ~scanf("%d%lld", &N, &M); ) { A.resize(N + 1); for (int u = 2; u <= N; ++u) { scanf("%d", &A[u]); --A[u]; } scanf("%d", &Q); X.resize(Q); Y.resize(Q); for (int q = 0; q < Q; ++q) { scanf("%d%d", &X[q], &Y[q]); } AA.assign(N + 1, 0); for (int u = 2; u <= N; ++u) { AA[u] = Mint(A[u]).pow((M - 3) + 2); } ans = 0; goal.assign(N + 1, 0); sums.assign(N + 1, 0); for (int u = 2; u <= N; ++u) { for (int v = 2 * u; v <= N; v += u) { add(u, v); } } printf("%d\n", ans.x); for (int q = 0; q < Q; ++q) { add(X[q], Y[q]); printf("%d\n", ans.x); } } return 0; }