#include using namespace std; const long long MOD = 1000000007; array, 3> matmul(array, 3> &A, array, 3> &B){ array, 3> ans; for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ ans[i][j] = 0; } } for (int i = 0; i < 3; i++){ for (int k = 0; k < 3; k++){ for (int j = 0; j < 3; j++){ ans[i][k] += A[i][j] * B[j][k]; } ans[i][k] %= MOD; } } return ans; } array, 3> matexp(array, 3> &A, long long b){ array, 3> ans; for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ ans[i][j] = i == j; } } while (b > 0){ if (b % 2 == 1){ ans = matmul(ans, A); } A = matmul(A, A); b /= 2; } return ans; } long long get(vector &A, int M, int a, int b){ array, 3> mat; mat[0][0] = A[a - 1] - 1; mat[0][1] = 1; mat[0][2] = 0; mat[1][0] = 0; mat[1][1] = A[b - 1] - 1; mat[1][2] = 1; mat[2][0] = 0; mat[2][1] = 0; mat[2][2] = A.back() - 1; mat = matexp(mat, M - 1); return mat[0][2]; } int main(){ int N, M; cin >> N >> M; vector A(N, 0); for (int i = 1; i < N; i++){ cin >> A[i]; } long long ans = 0; vector> prev(N + 1); for (int a = 2; a < N; a++){ if (N % a == 0){ for (int b = a * 2; b < N; b += a){ prev[b].push_back(a); if (N % b == 0){ ans += get(A, M, a, b); if (ans >= MOD){ ans -= MOD; } } } } } cout << ans << endl; int Q; cin >> Q; set st; for (int i = 0; i < Q; i++){ int X, Y; cin >> X >> Y; if (Y == N){ for (int a : prev[X]){ ans += get(A, M, a, X); if (ans >= MOD){ ans -= MOD; } } st.insert(X); } else { if (st.count(Y)){ ans += get(A, M, X, Y); if (ans >= MOD){ ans -= MOD; } } } prev[Y].push_back(X); cout << ans << endl; } }