#include #include using namespace std; #define rep(i, n) for(int i=0; i #include ll Power(ll x, ll y, ll B) { if (y == 0) { return 1; } if (y == 1) { return x % B; } if (y % 2 == 1) { return (Power(x, y - 1, B) * (x % B)) % B; } else { ll t = Power(x, y / 2, B) % B; return (t*t)%B; } } ll N, B; ll n = 1; struct SegTree { vector Tree; SegTree() { while (n < N) { n *= 2; } vector t(n*2, 0); Tree = t; } void Add(int l, int r){ AddFunc(l, r, 0, 0, n); } void AddFunc(int l, int r, int k, int s, int t) { if (r<=s || l>=t) { return; } else if (l <= s && r >= t) { Tree[k]++; } else { AddFunc(l, r, k * 2 + 1, s, (s+t) / 2); AddFunc(l, r, k * 2 + 2, (s+t) / 2, t); } } int Query(int m) { int ans = 0; int p = m + n - 1; while (p>0) { ans += Tree[p]; p = (p - 1) / 2; } ans += Tree[0]; return ans; } }; int main() { int Q; cin >> N >> B >> Q; SegTree tree; rep(test, Q) { int L, M, R; cin >> L >> M >> R; L--; M--; tree.Add(L, R); int num = tree.Query(M); ll X, Y, Z; X = (num + 1)%B; Z = Power(3, num, B)%B; if (num == 0) { Y = 1; } else { Y = (Power(3, num, B) + Power(3, num - 1, B) * (ll)num * ((ll)num + 3)) % B; } cout << X << " " << Y << " " << Z << endl; } }