import sys input = sys.stdin.readline N,B,Q=map(int,input().split()) LMR=[list(map(int,input().split())) for _ in range(Q)] class BIT: def __init__(self, n:int): self.n = n self.size = 1<<(n-1).bit_length() self.bit = [0] * (self.size + 1) # self.data = [0] * (n) # 本来の配列 def sum(self, i:int): # [0,i] i+=1 s = 0 while i > 0: s += self.bit[i] i -= i & -i return s def sumrange(self, i:int, j:int): # [i,j) return self.sum(j-1) - self.sum(i-1) def add(self, i:int, x:int): if x==0:return # self.data[i] += x i+=1 while i <= self.size: self.bit[i] += x i += i & -i x,y,z=1,1,1 ans=[(1,1,1)] for _ in range(Q): x = (x+1)%B y = (y*3+2*x*z)%B z = 3*z%B ans.append((x,y,z)) bit = BIT(N+1) for l,m,r in LMR: bit.add(l-1,1) bit.add(r,-1) print(*ans[bit.sum(m-1)])