#include using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(x) (x).begin(), (x).end() using ll = long long; const ll MOD = 998244353; #include using namespace atcoder; using mint = modint998244353; ll k,n; struct G{ ll a,b,c,d; }; G f(G X,G Y){ G Z; Z.a=(X.a*Y.a+X.b*Y.c)%k; Z.b=(X.a*Y.b+X.b*Y.d)%k; Z.c=(X.c*Y.a+X.d*Y.c)%k; Z.d=(X.c*Y.b+X.d*Y.d)%k; if(Z.a<0)Z.a+=k; if(Z.b<0)Z.b+=k; if(Z.c<0)Z.c+=k; if(Z.d<0)Z.d+=k; return Z; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin>>k>>n; vector g(n); rep(i,n)cin>>g[i].a>>g[i].b>>g[i].c>>g[i].d; auto e=[](){ G Z; Z.a=1;Z.b=0;Z.c=0;Z.d=1; return Z; }; segtree seg(g); ll q;cin>>q; while(q--){ ll I,l,r;cin>>I>>l>>r; I--;l--;r--; G Y; cin>>Y.a>>Y.b>>Y.c>>Y.d; seg.set(I,Y); G ans=seg.prod(l,r+1); cout<