#include #include using namespace std; using mint = atcoder::modint; using ll = long long; template struct Matrix { std::array, N> A{}; Matrix() {} Matrix(const std::array, N> &M) : A(M){} Matrix(const std::vector> &M) { for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ A[i][j] = M[i][j]; } } } const std::array& operator[](int i) const { return A[i]; } std::array& operator[](int i) { return A[i]; } Matrix& operator+=(const Matrix& rhs) { for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ A[i][j] += rhs[i][j]; } } return *this; } Matrix& operator-=(const Matrix& rhs) { for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ A[i][j] -= rhs[i][j]; } } return *this; } Matrix& operator*=(const Matrix& rhs) { std::array, N> res{}; for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ for(size_t k = 0; k < N; k++){ res[i][j] += A[i][k] * rhs[k][j]; } } } swap(A, res); return *this; } Matrix& operator+() const { return *this; } Matrix& operator-() const { return Matrix() - *this; } friend Matrix operator+(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) += rhs; } friend Matrix operator-(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) -= rhs; } friend Matrix operator*(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) *= rhs; } friend bool operator==(const Matrix& lhs, const Matrix& rhs) { return (lhs.A == rhs.A); } friend bool operator!=(const Matrix& lhs, const Matrix& rhs) { return (lhs.A != rhs.A); } Matrix pow(long long v){ Matrix res, temp = A; for(size_t i = 0; i < N; i++)res[i][i] = 1; while(v){ if(v & 1)res *= temp; temp *= temp; v >>= 1; } return res; } friend std::ostream& operator << (std::ostream &os, const Matrix& rhs) noexcept { for(size_t i = 0; i < N; i++){ if(i) os << '\n'; for(size_t j = 0; j < N; j++){ os << (j ? " " : "") << rhs[i][j]; } } return os; } }; using S = Matrix; S op(S lhs, S rhs){ return lhs * rhs; } S e(){ return Matrix({{1, 0}, {0, 1}}); } int main(){ ios::sync_with_stdio(false); cin.tie(0); int K, N, v; cin >> K >> N; mint::set_mod(K); vector tmp(N); for(int i = 0; i < N; i++){ for(int j = 0; j < 2; j++){ for(int k = 0; k < 2; k++){ int v; cin >> v; tmp[i][j][k] = v; } } } Matrix T; atcoder::segtree seg(tmp); int Q; cin >> Q; while(Q--){ int p, l, r, v; cin >> p >> l >> r; p--, l--; for(int y = 0; y < 2; y++){ for(int x = 0; x < 2; x++){ cin >> v; T[y][x] = v; } } seg.set(p, T); auto ans = seg.prod(l, r); for(int y = 0; y < 2; y++){ for(int x = 0; x < 2; x++){ cout << ans[y][x].val() << (x == 1 ? '\n' : ' '); } } } }