#include using namespace std; #include "atcoder/modint" #include "atcoder/lazysegtree" using namespace atcoder; using mint=modint; using S=int; using F=pair; S op(S a, S b){ return a+b; } S e(){ return 0; } S mapping(F f, S x){ return f.first*x+f.second; } F composition(F f, F g){ return make_pair(f.first*g.first,f.first*g.second+f.second); } F id(){ return make_pair(1,0); } int main(void) { int n,b,q; cin >> n >> b >> q; mint::set_mod(b); vector v(n,1); lazy_segtree s(v); while(q--){ int l,m,r; cin >> l >> m >> r; --l; --m; --r; s.apply(l,r+1,make_pair(1,1)); auto ans=s.get(m); cout << mint(ans).val() << " " << (mint(3).pow(ans-2)*(mint(ans)*ans+ans+1)).val() << " " << (mint(3).pow(ans-1)).val() << endl; } return 0; }