#include using namespace std; using ll = long long; int bbb[200020]; int si; void init(int N) { si = N; fill(bbb, bbb + (N + 1), 0); } void add(int id, int a) { id ++; while (id <= si) { bbb[id] += a; id += (id & (-id)); } } int sum(int id) { int ret = 0; while (id) { ret += bbb[id]; id -= (id & (-id)); } return ret; } int main() { int N, Q; ll B; cin >> N >> B >> Q; init(N); ll X[200020], Y[200020], Z[200020]; X[0] = Y[0] = Z[0] = 1; for (int i = 1; i <= Q; i ++) { X[i + 1] = (X[i] + 1) % B; Y[i + 1] = (3 * Y[i] + X[i + 1] * Z[i]) % B; Z[i + 1] = (3 * Z[i]) % B; } while (Q--) { int l, m, r; cin >> l >> m >> r; add(--l, 1); add(r, -1); int x = sum(m); printf("%lld %lld %lld\n", X[x], Y[x], Z[x]); } }