class SegmentTree: def __init__(self, ini, op, e): self.n = len(ini) self.op = op self.e = e self.v = [e] * (2 * self.n) for i in range(self.n): self.v[self.n + i] = ini[i] for i in range(self.n - 1, 0, -1): self.v[i] = op(self.v[2 * i], self.v[2 * i + 1]) def set(self, i, x): i += self.n self.v[i] = x while i > 1: i >>= 1 self.v[i] = self.op(self.v[2 * i], self.v[2 * i + 1]) def prod(self, l, r): resl = self.e resr = self.e l += self.n r += self.n while l < r: if l & 1: resl = self.op(resl, self.v[l]) l += 1 if r & 1: resr = self.op(self.v[r - 1], resr) l >>= 1 r >>= 1 return self.op(resl, resr) def get(self, i): return self.v[i + self.n] def dump(self): print(self.v[self.n:]) def op(x, y): return x * y e = 1 N, B, Q = map(int, input().split()) A = list(map(int, input().split())) seg = SegmentTree(A, op, e) for _ in range(Q): j, m, l, r = map(int, input().split()) x = seg.get(j) if x % B == 0 and m == B: x //= B else: x *= m seg.set(j, x) print(seg.prod(l, r + 1) % B)