class Fenwick_Tree: def __init__(self, n): self._n = n self.data = [0] * n def add(self, p, x): assert 0 <= p < self._n p += 1 while p <= self._n: self.data[p - 1] += x p += p & -p def sum(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s n, b, q = map(int, input().split()) f = Fenwick_Tree(n + 1) for i in range(q): l, m, r = map(int, input().split()) f.add(l - 1, 1) f.add(r, -1) x = f.sum(m - 1) print((x + 1) % b, pow(3, x - 1, b) * (x * x + 3 * x + 3) % b, pow(3, x, b))