n, b, q = map(int, input().split()) c = [0] * (n + 1) m = [0] * q for i in range(q): l, m[i], r = map(int, input().split()) c[l - 1] += 1 c[r] -= 1 for i in range(n): c[i + 1] += c[i] for i in m: x = c[i - 1] print((x + 1) % b, pow(3, x - 1, b) * (x * x + 3 * x + 3) % b, pow(3, x, b))