/* あのさぁ・・・ */ #include #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) using namespace std; template ostream& operator<<(ostream& o,const pair &p){return o<<"("< ostream& operator<<(ostream& o,const vector &vc){o<<"sz = "< struct ModInt{ using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr static uint mod = mod_; uint v; ModInt():v(0){} ModInt(ll v):v(normS(v%mod+mod)){} explicit operator bool() const {return v!=0;} static uint normS(const uint &x){return (x [0 , mod-1] static ModInt make(const uint &x){ModInt m; m.v=x; return m;} ModInt operator+(const ModInt& b) const { return make(normS(v+b.v));} ModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));} ModInt operator-() const { return make(normS(mod-v)); } ModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);} ModInt operator/(const ModInt& b) const { return *this*b.inv();} ModInt& operator+=(const ModInt& b){ return *this=*this+b;} ModInt& operator-=(const ModInt& b){ return *this=*this-b;} ModInt& operator*=(const ModInt& b){ return *this=*this*b;} ModInt& operator/=(const ModInt& b){ return *this=*this/b;} ll extgcd(ll a,ll b,ll &x,ll &y) const{ ll u[]={a,1,0},v[]={b,0,1}; while(*v){ ll t=*u/ *v; rep(i,3) swap(u[i]-=t*v[i],v[i]); } if(u[0]<0) rep(i,3) u[i]=-u[i]; x=u[1],y=u[2]; return u[0]; } ModInt inv() const{ ll x,y; extgcd(v,mod,x,y); return make(normS(x+mod)); } bool operator==(const ModInt& b) const { return v==b.v;} bool operator!=(const ModInt& b) const { return v!=b.v;} friend istream& operator>>(istream &o,ModInt& x){ ll tmp; o>>tmp; x=ModInt(tmp); return o; } friend ostream& operator<<(ostream &o,const ModInt& x){ return o<; template struct Matrix{ int H,W; vector< vector > m; Matrix():H(0),W(0){} Matrix(int H,int W):H(H),W(W),m( vector< vector >(H,vector(W,0)) ){} vector const& operator[](const int& i) const {return m[i];} vector& operator[](const int& i) {return m[i];} Matrix operator+=(const Matrix& b){ assert(H==b.H&&W==b.W); rep(i,H) rep(j,W) m[i][j]+=b[i][j]; return *this; } Matrix operator-=(const Matrix& b){ assert(H==b.H&&W==b.W); rep(i,H) rep(j,H) m[i][j]-=b[i][j]; 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 { assert(W==b.H); Matrix c(H,b.W); rep(i,H) rep(j,b.W) rep(k,W) c[i][j]+=m[i][k]*b[k][j]; return c; } Matrix operator*=(const Matrix& b){return (*this)=(*this)*b;} vector operator*(const vector& b) const { assert(W==b.size()); vector c(H); rep(i,H) rep(j,W) c[i]+=m[i][j]*b[j]; return c; } static Matrix E(int N){ Matrix a(N,N); rep(i,N) a[i][i]=1; return a; } friend ostream& operator<<(ostream &o,const Matrix& m){ o< struct segtree_simple{ int N; vector val; segtree_simple(){} segtree_simple(int n){ N=1; while(N& ds){ int n = ds.size(); N=1; while(N0;i--) val[i] = val[i*2] + val[i*2+1]; } void assign(int k,D d){ k+=N; val[k]=d; k/=2; while(k){ val[k] = val[k*2] + val[k*2+1]; k/=2; } } D query(int a,int b){ //non-commutative & unrecursive D L = D::e() , R = D::e(); a+=N,b+=N; while(a a; MA(){} MA(Matrix a):a(a){} const static MA e(){ return Matrix::E(N); } MA operator+(const MA& r) const { return MA(r.a*a); }; }; struct MB{ static const int N = 2; Matrix a; MB(){} MB(Matrix a):a(a){} const static MB e(){ return Matrix::E(N); } MB operator+(const MB& r) const { return MB(a*r.a); }; }; struct D{ //? -> b?a + x using Mat = Matrix; Mat b,a,x; D(){*this = e();} D(Mat b,Mat a,Mat x):b(b),a(a),x(x){} const static D e(){ return D(Mat::E(2),Mat::E(3),Mat(2,3)); } D operator+(const D& r) const { // puts("plus"); // show(b); // show(r.x); // show(b*r.x); auto ret = D(b*r.b,r.a*a,b*r.x*a+x); // puts("done"); return ret; }; }; Matrix X(int x){ Matrix res(2,3); rep(i,2) rep(j,3) res[i][j] = mint(6*x+i*3+j); return res; } segtree_simple sega; segtree_simple segb; segtree_simple segd; int main(){ int N; cin>>N; vector a0,bn; rep(i,3){ int x; cin>>x; a0.pb(mint(x)); } rep(i,2){ int x; cin>>x; bn.pb(mint(x)); } sega = segtree_simple(N+1); segb = segtree_simple(N+1); segd = [&]{ vector vs; rep1(i,N+1){ D v; v.x = X(i); vs.pb(v); } return segtree_simple(vs); }(); int Q; cin>>Q; rep(qt,Q){ string s; cin>>s; if(s == "a"){ int i; cin>>i; Matrix x(3,3); rep(i,3) rep(j,3){ int v; cin>>v; x.m[i][j] = mint(v); } MA A(x); sega.assign(i,A); MB B = segb.query(i,i+1); D v(B.a,A.a,B.a*X(i+1)*A.a); segd.assign(i,v); } if(s == "b"){ int i; cin>>i; Matrix x(2,2); rep(i,2) rep(j,2){ int v; cin>>v; x.m[i][j] = mint(v); } MB B(x); segb.assign(i,B); MA A = sega.query(i,i+1); D v(B.a,A.a,B.a*X(i+1)*A.a); segd.assign(i,v); } if(s == "ga"){ int i; cin>>i; auto v = sega.query(0,i).a * a0; cout<>i; if(i == N){ cout<