import re import sys sys.setrecursionlimit(100000) class FenwickTree: def __init__(self, n): self.n = n self.v = [0] * (n + 1) def sum(self, r): res = 0 while r > 0: res += self.v[r] r -= r & -r return res def add(self, i, x): i += 1 while i <= self.n: self.v[i] += x i += i & -i content = input() pattern = r'^(\d+) (\d+) (\d+)$' result = re.match(pattern, content) if not result: sys.exit("format error.") N, B, Q = map(int, result.groups()) if not (1 <= N and N <= 10**5): sys.exit("N") if not (1 <= B and B <= 10**9): sys.exit("B") if not (1 <= Q and Q <= 10**5): sys.exit("Q") x = [0] * (Q + 1) y = [0] * (Q + 1) z = [0] * (Q + 1) x[0] = 1 % B y[0] = 1 % B z[0] = 1 % B for j in range(Q): x[j + 1] = (x[j] + 1) % B y[j + 1] = (3 * y[j] + 2 * x[j + 1] * z[j]) % B z[j + 1] = (3 * z[j]) % B ft = FenwickTree(N + 1) for _ in range(Q): content = input() pattern = r'^(\d+) (\d+) (\d+)$' result = re.match(pattern, content) if not result: sys.exit("format error.") l, m, r = map(int, result.groups()) if not (1 <= l and l <= m and m <= r and r <= N): sys.exit("l m r") l -= 1 ft.add(l, 1) ft.add(r, -1) j = ft.sum(m) print(x[j], y[j], z[j])