import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### class DualSegmentTree: def __init__(self, size, f, default): self.n = (size-1).bit_length() self.size = 1<>i) def update(self, a, b, x): a += self.size b += self.size-1 self.thrust(a) self.thrust(b) l = a r = b + 1 lazy = self.lazy while l < r: if l & 1: lazy[l] = self.f(lazy[l], x) l += 1 if r & 1: r -= 1 lazy[r] = self.f(lazy[r], x) l >>= 1 r >>= 1 def get(self, k): k += self.size self.thrust(k) return self.lazy[k] n,b,q = na() m = 10**5+10 x = [1%b] * (m) y = [1%b] * m z = [1%b] * m for i in range(1, m): x[i] = (x[i-1] + 1)%b y[i] = (3*y[i-1]+2*x[i]*z[i-1])%b z[i] = z[i-1] * 3 % b dst = DualSegmentTree(n, lambda x,y:x+y, 0) #print(x[:10]) #print(y[:10]) #print(z[:10]) for i in range(q): l,m,r = na() l-=1 m-=1 dst.update(l, r, 1) a = dst.get(m) print(x[a], y[a], z[a])