#include // #include "atcoder/modint" using i64 = long long; // using Fp = atcoder::modint998244353; #include "atcoder/lazysegtree" int op(int a, int b) { return a + b; } int e() { return 0; } int main() { int N, Q; i64 B; std::cin >> N >> B >> Q; std::vector X(Q + 1), Y(Q + 1), Z(Q + 1); X[0] = Y[0] = Z[0] = 1; for (int i = 0; i < Q; ++i) { X[i + 1] = X[i] + 1; Y[i + 1] = 3 * Y[i] + 2 * X[i + 1] * Z[i]; Z[i + 1] = 3 * Z[i]; X[i + 1] %= B; Y[i + 1] %= B; Z[i + 1] %= B; } atcoder::lazy_segtree seg(N); while (Q--) { int l, r, m; std::cin >> l >> m >> r; --l, --m; seg.apply(l, r, 1); const auto i = seg.get(m); std::cout << X[i] << ' ' << Y[i] << ' ' << Z[i] << std::endl; } }