#include #include #include using namespace std; map factor(int n) { map ret; for (int i = 2; i * i <= n; ++i) { while (n % i == 0) { ++ret[i]; n /= i; } } if (1 < n) { ++ret[n]; } return ret; } using atcoder::modint; vector ps; struct S { modint r; vector e; }; S op(S x, S y) { auto e = x.e; for (size_t j = 0; j < size(e); ++j) { e[j] += y.e[j]; } return {x.r * y.r, e}; } S e() { return {1, vector(size(ps))}; } S makeS(int64_t A) { auto s = e(); for (size_t j = 0; j < size(ps); ++j) { while (A % ps[j] == 0) { ++s.e[j]; A /= ps[j]; } } s.r = A; return s; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, B, Q; cin >> N >> B >> Q; modint::set_mod(B); for (auto [p, e] : factor(B)) { ps.push_back(p); } atcoder::segtree seg(N); for (int i = 0; i < N; ++i) { int64_t A; cin >> A; seg.set(i, makeS(A)); } while (Q--) { int i; int64_t m; int l, r; cin >> i >> m >> l >> r; ++r; auto s = makeS(m); if (m == B) { bool multiple = true; auto e = seg.get(i).e; for (size_t j = 0; j < size(ps); ++j) { multiple &= s.e[j] <= e[j]; } if (multiple) { for (int& x : s.e) { x = -x; } } } seg.set(i, op(seg.get(i), s)); s = seg.prod(l, r); auto ans = s.r; for (size_t j = 0; j < size(ps); ++j) { // ans *= modint(ps[j]).pow(s.e[j]); } cout << ans.val() << '\n'; } }