#line 1 "template/template.hpp" #include using namespace std; using int64 = long long; const int mod = 998244353; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 >& p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { explicit FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } #line 1 "math/combinatorics/mod-pow.hpp" /** * @brief Mod Pow(べき乗) * @docs docs/mod-pow.md */ template< typename T > T mod_pow(T x, int64_t n, const T &p) { T ret = 1; while(n > 0) { if(n & 1) (ret *= x) %= p; (x *= x) %= p; n >>= 1; } return ret % p; } #line 1 "math/number-theory/prime-factor.hpp" map< int64_t, int > prime_factor(int64_t n) { map< int64_t, int > ret; for(int64_t i = 2; i * i <= n; i++) { while(n % i == 0) { ret[i]++; n /= i; } } if(n != 1) ret[n] = 1; return ret; } #line 1 "structure/segment-tree/segment-tree.hpp" /** * @brief Segment Tree(セグメント木) * @docs docs/segment-tree.md */ template< typename T, typename F > struct SegmentTree { int n, sz; vector< T > seg; const F f; const T ti; SegmentTree() = default; explicit SegmentTree(int n, const F f, const T &ti) : n(n), f(f), ti(ti) { sz = 1; while(sz < n) sz <<= 1; seg.assign(2 * sz, ti); } explicit SegmentTree(const vector< T > &v, const F f, const T &ti) : SegmentTree((int) v.size(), f, ti) { build(v); } void build(const vector< T > &v) { assert(n == (int) v.size()); for(int k = 0; k < n; k++) seg[k + sz] = v[k]; for(int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void set(int k, const T &x) { k += sz; seg[k] = x; while(k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } T get(int k) const { return seg[k + sz]; } T operator[](const int &k) const { return get(k); } void apply(int k, const T &x) { k += sz; seg[k] = f(seg[k], x); while(k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } T prod(int l, int r) const { T L = ti, R = ti; for(l += sz, r += sz; l < r; l >>= 1, r >>= 1) { if(l & 1) L = f(L, seg[l++]); if(r & 1) R = f(seg[--r], R); } return f(L, R); } T all_prod() const { return seg[1]; } template< typename C > int find_first(int l, const C &check) const { if(l >= n) return n; l += sz; T sum = ti; do { while((l & 1) == 0) l >>= 1; if(check(f(sum, seg[l]))) { while(l < sz) { l <<= 1; auto nxt = f(sum, seg[l]); if(not check(nxt)) { sum = nxt; l++; } } return l + 1 - sz; } sum = f(sum, seg[l++]); } while((l & -l) != l); return n; } template< typename C > int find_last(int r, const C &check) const { if(r <= 0) return -1; r += sz; T sum = ti; do { r--; while(r > 1 and (r & 1)) r >>= 1; if(check(f(seg[r], sum))) { while(r < sz) { r = (r << 1) + 1; auto nxt = f(seg[r], sum); if(not check(nxt)) { sum = nxt; r--; } } return r - sz; } sum = f(seg[r], sum); } while((r & -r) != r); return -1; } }; template< typename T, typename F > SegmentTree< T, F > get_segment_tree(int N, const F &f, const T &ti) { return SegmentTree{N, f, ti}; } template< typename T, typename F > SegmentTree< T, F > get_segment_tree(const vector< T > &v, const F &f, const T &ti) { return SegmentTree{v, f, ti}; } int main() { int N, B, Q; cin >> N >> B >> Q; vector< int64 > A(N); cin >> A; auto P_ = prime_factor(B); vector< pair< int64, int > > P(P_.begin(), P_.end()); auto as = make_v< int >(P.size(), N); for(int i = 0; i < N; i++) { for(int j = 0; j < P.size(); j++) { while(A[j] % P[j].first == 0) { A[j] /= P[j].first; ++as[j][i]; } } } auto f = [&](int64 a, int64 b) { return a * b % B; }; auto seg = get_segment_tree(A, f, 1ll); auto g = [](int a, int b) { return a + b; }; vector< SegmentTree< int, decltype(g) > > segs; segs.reserve(P.size()); for(int i = 0; i < P.size(); i++) { segs.emplace_back(as[i], g, 0); } vector< vector< int64 > > stocks(P.size(), vector< int64 >(1, 1)); while(Q--) { int i, l, r; int64 m; cin >> i >> m >> l >> r; ++r; bool is_mul = true; for(int j = 0; j < P.size(); j++) { is_mul &= as[j][i] >= P[j].second; } if(is_mul and m == B) { for(int j = 0; j < P.size(); j++) { as[j][i] -= P[j].second; segs[j].set(i, as[j][i]); } } else { for(int j = 0; j < P.size(); j++) { while(m % P[j].first == 0) { m /= P[j].first; ++as[j][i]; } segs[j].set(i, as[j][i]); } (A[i] *= m % B) %= B; seg.set(i, A[i]); } auto ret = seg.prod(l, r); for(int j = 0; j < P.size(); j++) { auto res = segs[j].prod(l, r); while(stocks[j].size() <= res) { int64 nxt = (stocks[j].back() * P[j].first) % B; stocks[j].emplace_back(nxt); } (ret *= stocks[j][res]) %= B; } cout << ret << "\n"; } }