#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>; using mint = atcoder::modint; int main() { FAST_IO int n,b,q; cin >> n >> b >> q; mint::set_mod(b); using M = vector>; auto op = [](M x, M y) { M z(2, vector(2, 0)); for (auto i : views::iota(0, 2)) { for (auto j : views::iota(0, 2)) { for (auto k : views::iota(0, 2)) { z[i][j] += y[i][k] * x[k][j]; } } } return z; }; auto e = []() { M I(2, vector(2, 0)); I[0][0] = I[1][1] = 1; return I; }; atcoder::segtree st(n); for (auto i : views::iota(0, n)) { int a1,a2,a3,a4; cin >> a1 >> a2 >> a3 >> a4; M a(2, vector(2, 0)); a[0][0] = a1; a[0][1] = a2; a[1][0] = a3; a[1][1] = a4; st.set(i, a); } for (auto i : views::iota(0,q)) { int l,r,x,y; cin >> l >> r >> x >> y; M v(2, vector(2, 0)); v[0][0] = x; v[1][0] = y; auto ret = op(v, st.prod(l, r)); cout << ret[0][0].val() << " " << ret[1][0].val() << endl; } }