#include #include using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define FAST_IO \ ios::sync_with_stdio(false); \ cin.tie(0); const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; template struct Matrix { using Array = array, H>; Array A; Matrix() : A() { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) (*this)[i][j] = T(); } int height() const { return H; } int width() const { return W; } inline const array &operator[](int k) const { return A[k]; } inline array &operator[](int k) { return A[k]; } static Matrix I() { assert(H == W); Matrix mat; for (int i = 0; i < H; i++) mat[i][i] = 1; return (mat); } Matrix &operator+=(const Matrix &B) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) A[i][j] += B[i][j]; return (*this); } Matrix &operator-=(const Matrix &B) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) A[i][j] -= B[i][j]; return (*this); } Matrix &operator*=(const Matrix &B) { assert(H == W); Matrix C; for (int i = 0; i < H; i++) for (int k = 0; k < H; k++) for (int j = 0; j < H; j++) C[i][j] += A[i][k] * B[k][j]; A.swap(C.A); return (*this); } Matrix &operator^=(long long k) { Matrix B = Matrix::I(); while (k > 0) { if (k & 1) B *= *this; *this *= *this; k >>= 1LL; } A.swap(B.A); return (*this); } Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); } Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); } Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); } Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); } bool operator==(const Matrix &B) const { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (A[i][j] != B[i][j]) return false; return true; } bool operator!=(const Matrix &B) const { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (A[i][j] != B[i][j]) return true; return false; } friend ostream &operator<<(ostream &os, const Matrix &p) { for (int i = 0; i < H; i++) { os << "["; for (int j = 0; j < W; j++) { os << p[i][j] << (j + 1 == W ? "]\n" : ","); } } return (os); } T determinant(int n = -1) { if (n == -1) n = H; Matrix B(*this); T ret = 1; for (int i = 0; i < n; i++) { int idx = -1; for (int j = i; j < n; j++) { if (B[j][i] != 0) { idx = j; break; } } if (idx == -1) return 0; if (i != idx) { ret *= T(-1); swap(B[i], B[idx]); } ret *= B[i][i]; T inv = T(1) / B[i][i]; for (int j = 0; j < n; j++) { B[i][j] *= inv; } for (int j = i + 1; j < n; j++) { T a = B[j][i]; if (a == 0) continue; for (int k = i; k < n; k++) { B[j][k] -= B[i][k] * a; } } } return (ret); } }; using mint = atcoder::modint; using M = Matrix; int main() { FAST_IO int n,b,q; cin >> n >> b >> q; mint::set_mod(b); auto op = [&](M x, M y) { return y * x; }; auto e = [&]() { M I; I[0][0] = I[1][1] = 1; return I; }; atcoder::segtree st(n); for (int i = 0; i < n; i ++) { int a1,a2,a3,a4; cin >> a1 >> a2 >> a3 >> a4; M a; a[0][0] = a1; a[0][1] = a2; a[1][0] = a3; a[1][1] = a4; st.set(i, a); } for (int i = 0; i < q; i ++) { int l,r,x,y; cin >> l >> r >> x >> y; M v; v[0][0] = x; v[1][0] = y; auto ret = st.prod(l, r) * v; cout << ret[0][0].val() << " " << ret[1][0].val() << endl; } }