using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
static int NN => int.Parse(ReadLine());
static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
public static void Main()
{
Solve();
}
static void Solve()
{
var c = NList;
var (n, b, q) = (c[0], c[1], (int)c[2]);
var map = NArr(q);
var x = new long[q + 1];
var y = new long[q + 1];
var z = new long[q + 1];
x[0] = 1;
y[0] = 1;
z[0] = 1;
var seg = new LazyAddSumSegmentTree(new int[n]);
var ans = new string[q];
for (var i = 0; i < q; ++i)
{
x[i + 1] = (x[i] + 1) % b;
y[i + 1] = (3 * y[i] % b + 2 * x[i + 1] % b * z[i] % b) % b;
z[i + 1] = 3 * z[i] % b;
seg.AddRange(map[i][0] - 1, map[i][2], 1);
var time = (int)seg.Get(map[i][1] - 1, map[i][1]);
ans[i] = $"{x[time]} {y[time]} {z[time]}";
}
WriteLine(string.Join("\n", ans));
}
class LazyAddSumSegmentTree
{
public long[] tree;
public long[] lazy;
/// 範囲外を参照したときの値 欲しい値の種類によって変える
long INF = 0;
int size;
public LazyAddSumSegmentTree(int[] list)
{
size = 1;
while (size < list.Length) size <<= 1;
tree = Enumerable.Repeat(INF, size * 2).ToArray();
lazy = Enumerable.Repeat(INF, size * 2).ToArray();
for (var i = 0; i < list.Length; ++i) tree[i + size] = list[i];
for (var i = size - 1; i >= 1; --i) tree[i] = Calc(tree[i * 2], tree[i * 2 + 1]);
}
/// 取得時の評価 欲しい値の種類によって変える
long Calc(long a, long b)
{
return a + b;
}
/// 遅延評価の値を適用する 区間に対する操作によって変える
long Leval(long tval, long lval)
{
return tval + lval;
}
void Eval(int k)
{
if (lazy[k] == INF) return;
if (k < size)
{
lazy[k * 2] = Calc(lazy[k * 2], lazy[k] / 2);
lazy[k * 2 + 1] = Calc(lazy[k * 2 + 1], lazy[k] / 2);
}
tree[k] = Leval(tree[k], lazy[k]);
lazy[k] = INF;
}
/// 区間加算(end is not include)
public void AddRange(int begin, int end, long value, int k = 1, int l = 0, int r = -1)
{
if (r < 0) r = size;
Eval(k);
if (begin <= l && r <= end)
{
lazy[k] += (r - l) * value;
Eval(k);
}
else if (begin < r && l < end)
{
AddRange(begin, end, value, k * 2, l, (l + r) / 2);
AddRange(begin, end, value, k * 2 + 1, (l + r) / 2, r);
tree[k] = Calc(tree[k * 2], tree[k * 2 + 1]);
}
}
/// 値の取得(end is not include)
public long Get(int begin, int end, int k = 1, int l = 0, int r = -1)
{
if (r < 0) r = size;
Eval(k);
if (begin <= l && r <= end) return tree[k];
else if (begin < r && l < end)
return Calc(Get(begin, end, k * 2, l, (l + r) / 2), Get(begin, end, k * 2 + 1, (l + r) / 2, r));
else return INF;
}
}
}