import sys # sys.setrecursionlimit(200005) # sys.set_int_max_str_digits(1000005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # md = 10**9+7 md = 998244353 class BitSum: def __init__(self, n): self._n = n+1 self._table = [0]*self._n self._origin = [0]*n def __getitem__(self, item): return self._origin[item] def add(self, i, x): self._origin[i] += x i += 1 while i < self._n: self._table[i] += x i += i & -i def sum(self, i): i += 1 res = 0 while i > 0: res += self._table[i] i -= i & -i return res def sumlr(self, l, r): if l >= r: return 0 if l == 0: return self.sum(r-1) return self.sum(r-1)-self.sum(l-1) # return "min(i) of sum(i)>=x" or "N" def bisect(self, x): idx = 0 d = 1 << (self._n-1).bit_length()-1 while d: if idx+d < self._n and self._table[idx+d] < x: x -= self._table[idx+d] idx |= d d >>= 1 return idx n,b,q=LI() if b==1: for _ in range(q):print(0,0,0) exit() xx=[(a+1)%b for a in range(n+1)] zz=[1] for _ in range(n):zz.append(zz[-1]*3%b) yy=[1]*(n+1) for i in range(1,n+1): yy[i]=yy[i-1]*3%b+2*xx[i]*zz[i-1]%b yy[i]%=b bit=BitSum(n+1) for _ in range(q): l,m,r=LI() l,m=l-1,m-1 bit.add(l,1) bit.add(r,-1) i=bit.sum(m) print(xx[i],yy[i],zz[i])