#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } using mint = atcoder::modint; ostream& operator<<(ostream& os, const mint& m){ os << m.val(); return os; } //1-indexed template struct bit{ int n; vector data; bit(int n_){ n = 1; while(n < n_) n *= 2; data = vector(n+1); for(int i = 0; i <= n; i++) data[i] = 0; } T sum(int i){ T ret = 0; while(i > 0){ ret += data[i]; i -= i&-i; } return ret; } void add(int i, T x){ while(i <= n){ data[i] += x; i += i&-i; } } }; template class Matrix { public: array, N> dat; Matrix(T val) { for(int i = 0; i < N; i++){ for(int j = 0; j < M; j++){ dat[i][j] = val; } } } Matrix(array, N> dat): dat(dat){ } array& operator[](int x) { return dat[x]; } }; template Matrix operator*(Matrix a, Matrix b){ Matrix c(T(0)); for(int i = 0; i < N; i++){ for(int j = 0; j < K; j++){ for(int k = 0; k < M; k++){ c.dat[i][j] += a.dat[i][k]*b.dat[k][j]; } } } return c; } template Matrix operator^(Matrix m, long long r){ Matrix ans(T(0)); for(int i = 0; i < N; i++) ans[i][i] = T(1); while (r > 0) { if (r&1) ans = (ans*m); m = (m*m); r >>= 1; } return ans; } template void print_mat(Matrix a){ for(int i = 0; i < N; i++){ for(int j = 0; j < M; j++){ cout << a.dat[i][j] << ' '; } cout << endl; } } template ostream& operator<<(ostream& os, const Matrix& m){ print_mat(m); return os; } using Mat = Matrix; using Vec = Matrix; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n, b, q; cin >> n >> b >> q; mint::set_mod(b); vector v; Mat e(mint(0)); Mat a(mint(0)); for(int i = 0; i < 5; i++) e[i][i] = 1; a[0][0] = 1; a[0][4] = 1; a[1][1] = 3; a[1][2] = 2; a[1][3] = 2; a[2][2] = 3; a[3][2] = 3; a[3][3] = 3; a[4][4] = 1; v.push_back(e); auto x = e; for(int i = 0; i < 100000; i++){ x = x*a; v.push_back(x); } bit bt(n*2); Vec u(mint(1));; while(q--){ int l, m, r; cin >> l >> m >> r; bt.add(l, 1); bt.add(r+1, -1); int x = bt.sum(m); auto ans = v[x]*u; cout << ans[0][0] << ' ' << ans[1][0] << ' ' << ans[2][0] << '\n'; } }