#include #define debug(x) cerr << #x << ": " << x << '\n' #define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n' using namespace std; typedef long long ll; typedef unsigned long long ull; typedef tuple Pll; typedef vector vll; const ll INF = LLONG_MAX/10; const ll MOD = 1e9+7; ll extgcd(ll a, ll b, ll &x, ll &y) { ll g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; } ll invMod(ll a, ll m) { ll x, y; if (extgcd(a, m, x, y) == 1) return (x + m) % m; else return 0; // unsolvable } template struct Matrix { vector > val; Matrix(int n, int m, long long x = 0) : val(n, vector(m, x)) {} void init(int n, int m, long long x = 0) {val.assign(n, vector(m, x));} size_t size() const {return val.size();} inline vector& operator [] (int i) {return val[i];} Matrix& operator=(const Matrix b){ for(ll i=0;i<(ll)val.size();i++){ for(ll j=0;j<(ll)val[i].size();j++){ val[i][j]=b.val[i][j]; } } return *this; } }; // O( n ) template Matrix identity(int n) { Matrix A(n, n); for (int i = 0; i < n; ++i) A[i][i] = 1; return A; } // O( n^3 ) template Matrix mul(Matrix A, Matrix B) { Matrix C(A.size(), B[0].size()); for (int i = 0; i < (ll)C.size(); ++i) for (int j = 0; j <(ll) C[i].size(); ++j) for (int k = 0; k < (ll)A[i].size(); ++k) C[i][j] = (C[i][j] + A[i][k] * B[k][j]%mod)%mod; return C; } // O( n^3 log e ) template Matrix pow(Matrix A, int e) { return e == 0 ? identity(A.size()) : e % 2 == 0 ? pow(mul(A, A), e/2) : mul(A, pow(A, e-1)); } template struct SegmentTree{ using F = function; int n; F f; T ti; vector dat; SegmentTree(){}; SegmentTree(F f,T ti):f(f),ti(ti){} void init(int n_){ n=1; while(n &v){ int n_=v.size(); init(n_); for(int i=0;i>=1) dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]); } //[a,b) T query(int a,int b,int k=1,int l=0,int r=-1){ if(r<0)r=n; if(r<=a||b<=l) return ti; if(a<=l&&r<=b) return dat[k]; T vl=query(a,b,k*2,l,(l+r)/2); T vr=query(a,b,k*2+1,(l+r)/2,r); return f(vl,vr); } T operator[](const int &k) const {return dat[k + n];} }; struct HLDecomposition{ private: int t; void dfs_size(int v=0){ size[v]=1; for(int& u:g[v]){ if(par[u]>=0){ swap(u,g[v].back()); if(u==g[v].back())continue; } par[u]=v; dfs_size(u); size[v]+=size[u]; if(size[u]>size[g[v][0]]){ swap(u,g[v][0]); } } } void dfs_hld(int v=0){ in[v] = t++; inv[in[v]]=v; for(int& u:g[v]){ if(par[u]!=v)continue; head[u]=(u==g[v][0]?head[v]:u); dfs_hld(u); } out[v]=t; } public: int V; vector> g; vector dep,par,head,size,inv; vector in,out; HLDecomposition(int size_) :t(0),V(size_),g(V),dep(V,0),par(V,-1),head(V),size(V),inv(V),in(V),out(V){} void add_edge(int u,int v){ g[u].push_back(v); g[v].push_back(u); } void build(int root=0){ par[root]=0; dfs_size(root); par[root]=-1; dfs_hld(root); } int lca(int u,int v){ while(1){ if(in[u]>in[v])swap(u,v); if(head[u]==head[v])return u; v=par[head[v]]; } } int distance(int u,int v){ return dep[u]+dep[v]-2*dep[lca(u,v)]; } int operator[](const int &k){ return in[k]; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N;cin>>N; HLDecomposition hld(N); vector >E(N-1); for(ll i=0;i>a>>b; E[i]=make_tuple(a,b); hld.add_edge(a,b); } hld.build(0); //debugArray(hld,N); for(ll i=0;i a,Matrix b){ return mul(a,b); }; SegmentTree > seg(f,identity(2)); seg.init(N); ll Q;cin>>Q; for(ll q=0;q>op; if(op=='x'){ ll i; Matrix A(2,2); cin>>i>>A[0][0]>>A[0][1]>>A[1][0]>>A[1][1]; seg.set_val(hld[get<1>(E[i])],A); }else{ ll i,j;cin>>i>>j; Matrix ans=identity(2); while(1){ if(hld[i]>hld[j])swap(i,j); if(hld.head[i]!=hld.head[j]){ //debug(q); ans = mul(ans,seg.query(hld[hld.head[j]],hld[j]+1)); j=hld.par[hld.head[j]]; }else{ if(i!=j)ans = mul(ans,seg.query(hld[i]+1,hld[j]+1)); break; } } cout<