#include using namespace std; struct BIT { private: vector bit; int N; public: BIT(int size) { N = size; bit.resize(N + 1); } void add(int a, int w) { for(int x = a; x <= N; x += x & -x) { bit[x] += w; } } int sum(int a) { int ret = 0; for (int x = a; x > 0; x -= x & -x) { ret += bit[x]; } return ret; } }; int main() { long long N, B, Q; cin >> N >> B >> Q; BIT fw(N + 5); vector sq(Q + 1), s(Q + 1), t(Q + 2); sq[0] = 0; for(long long i = 1; i <= Q; i++) { sq[i] = i * i % B; s[i] = i % B; } t[2] = 1; for(int i = 3; i <= Q + 2; i++) { t[i] = t[i - 1] * 3 % B; } while(Q--) { int L, M, R; cin >> L >> M >> R; fw.add(L, 1); fw.add(R + 1, -1); int x = fw.sum(M) + 1; long long y; if(x == 1) { y = 1; } else { y = t[x] * (sq[x] + s[x] + 1) % B; } int z = t[x + 1]; cout << x % B << ' ' << y % B << ' ' << z % B << '\n'; } return 0; }