#pragma region Macros #pragma GCC optimize("O3") #include #define ll long long #define ld long double #define rep2(i,a,b) for(ll i=a;i<=b;++i) #define rep(i,n) for(ll i=0;i=b;i--) #define pii pair #define pll pair #define pb push_back #define eb emplace_back #define vi vector #define vec vector #define vll vector #define vpi vector #define vpll vector #define vec2(a,b) vector(a,vi(b)) #define vec2ll(a,b) vector(a,vll(b)) #define vec3(a,b,c) vector>(a,vec2(b,c)) #define vec3ll(a,b,c) vector>(a,vec2ll(b,c)) #define fi first #define se second #define all(c) begin(c),end(c) #define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); #define lb(c,x) distance((c).begin(),lower_bound(all(c),(x))) #define ub(c,x) distance((c).begin(),upper_bound(all(c),(x))) using namespace std; template using pq = priority_queue; template using pqg = priority_queue,greater>; #define INT(...) int __VA_ARGS__;IN(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;IN(__VA_ARGS__) #define ULL(...) ull __VA_ARGS__;IN(__VA_ARGS__) #define STR(...) string __VA_ARGS__;IN(__VA_ARGS__) #define CHR(...) char __VA_ARGS__;IN(__VA_ARGS__) #define DBL(...) double __VA_ARGS__;IN(__VA_ARGS__) #define LD(...) ld __VA_ARGS__;IN(__VA_ARGS__) #define VEC(type,name,size) vector name(size);IN(name) #define VV(type,name,h,w) vector>name(h,vector(w));IN(name) int scan(){ return getchar(); } void scan(int& a){ cin>>a; } void scan(long long& a){ cin>>a; } void scan(char &a){cin>>a;} void scan(double &a){ cin>>a; } void scan(long double& a){ cin>>a; } void scan(char a[]){ scanf("%s", a); } void scan(string& a){ cin >> a; } template void scan(vector&); template void scan(array&); template void scan(pair&); template void scan(T(&)[size]); template void scan(vector& a){ for(auto& i : a) scan(i); } template void scan(deque& a){ for(auto& i : a) scan(i); } template void scan(array& a){ for(auto& i : a) scan(i); } template void scan(pair& p){ scan(p.first); scan(p.second); } template void scan(T (&a)[size]){ for(auto& i : a) scan(i); } template void scan(T& a){ cin >> a; } void IN(){} template void IN(Head& head, Tail&... tail){ scan(head); IN(tail...); } string stin() {string s;cin>>s;return s;} template inline bool chmax(T& a,T b){if(a inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} vi iota(int n){vi a(n);iota(all(a),0);return a;} template void UNIQUE(vector &x){sort(all(x));x.erase(unique(all(x)),x.end());} int in() {int x;cin>>x;return x;} ll lin() {unsigned long long x;cin>>x;return x;} void print(){putchar(' ');} void print(bool a){cout< void print(const vector&); template void print(const array&); template void print(const pair& p); template void print(const T (&)[size]); template void print(const vector& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout<<" "; print(*i); } cout< void print(const deque& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout<<" "; print(*i); } } template void print(const array& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout<<" "; print(*i); } } template void print(const pair& p){ cout<<'(';print(p.first); cout<<","; print(p.second);cout<<')'; } template void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ cout<<" "; print(*i); } } template void print(const T& a){ cout << a; } int out(){ putchar('\n'); return 0; } template int out(const T& t){ print(t); putchar('\n'); return 0; } template int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; } ll gcd(ll a, ll b){ while(b){ ll c = b; b = a % b; a = c; } return a; } ll lcm(ll a, ll b){ if(!a || !b) return 0; return a * b / gcd(a, b); } vector factor(ll x){ vector ans; for(ll i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; } vector divisor(int x){ vector ans; for(int i=1;i*i<=x;i++)if(x%i==0){ans.pb(i);if(i*i!=x)ans.pb(x/i);} return ans;} int popcount(ll x){return __builtin_popcountll(x);} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int n){return uniform_int_distribution(0, n)(rng);} #define endl '\n' #ifdef _LOCAL #undef endl #define debug(x) cout<<#x<<": "< void err(const T& t){ print(t); cout<<" ";} template void err(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); } #else #define debug(x) template void err(const T&...){} #endif #pragma endregion template< typename Monoid > struct SegmentTree { using F = function< Monoid(Monoid, Monoid) >; int sz; vector< Monoid > seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) { sz = 1; while(sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid &x) { seg[k + sz] = x; } void build() { for(int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid &x) { k += sz; seg[k] = x; while(k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) L = f(L, seg[a++]); if(b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } template< typename C > int find_subtree(int a, const C &check, Monoid &M, bool type) { while(a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if(check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template< typename C > int find_first(int a, const C &check) { Monoid L = M1; if(a <= 0) { if(check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) { Monoid nxt = f(L, seg[a]); if(check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template< typename C > int find_last(int b, const C &check) { Monoid R = M1; if(b >= sz) { if(check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for(b += sz; a < b; a >>= 1, b >>= 1) { if(b & 1) { Monoid nxt = f(seg[--b], R); if(check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } void print(int n){ for(int i = 0; i < n; i++){ cerr << seg[i + sz] << " " ; }cerr << endl; } }; template< typename T > struct edge{ int from, to; T cost; edge(int to,T cost) : from(-1), to(to), cost(cost){} edge(int from,int to,T cost) : from(from), to(to), cost(cost){} edge &operator=(const int &x) { to = x; return *this; } operator int() const{ return to;} }; template< typename T > using Edges = vector< edge< T > >; template< typename T > using WeightedTree = vector< Edges>; using tree = vector< vector >; tree make(int n,int offset = 1){ tree res(n); for(int i = 0;i < n-1; i++){ int a,b; cin >> a >> b; a -= offset,b -= offset; res[a].emplace_back(b); res[b].emplace_back(a); } return res; } template< typename T > WeightedTree make2(int n, int offset = 1){ WeightedTree res(n); for(int i = 0;i < n-1 ; i++){ int a,b ; cin >> a >> b; a -= offset, b -= offset; T c; cin >> c; res[a].emplace_back(b,c); res[b].emplace_back(a,c); } return res; } template< typename G > struct HLDecomposition{ G &g; vector sz, in, out, head, rev, par; HLDecomposition(G &g): g(g), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) {} void dfs_sz(int idx, int p){ par[idx] = p; sz[idx] = 1; if(g[idx].size() and g[idx][0] == p) swap(g[idx][0], g[idx].back()); for(auto &to: g[idx]){ if(to == p) continue; dfs_sz(to,idx); sz[idx] += sz[to]; if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to); } } void dfs_hld(int idx, int par, int ×){ in[idx] = times++; rev[in[idx]] = idx; for(auto &to : g[idx]){ if(to == par) continue; head[to] = (g[idx][0] == to ? head[idx] :to); dfs_hld(to,idx,times); } out[idx] = times; } template< typename T > void dfs_hld(int idx, int par, int ×,vector &v){ in[idx] = times++; rev[in[idx]] = idx; for(auto &to : g[idx]){ if(to == par) { v[in[idx]] = to.cost; continue; } head[to] = (g[idx][0] == to ? head[idx] :to); dfs_hld(to,idx,times,v); } out[idx] = times; } void build(){ dfs_sz(0,-1); int t = 0; dfs_hld(0,-1,t); } template< typename T > vector< T > build(){ dfs_sz(0,-1); int t = 0; vector< T > res(g.size()); dfs_hld(0,-1,t,res); return res; } int la(int v,int k){ while(1){ int u = head[v]; if(in[v] - k >= in[u]) return rev[in[v] - k]; k -= in[v] - in[u] + 1; v = par[u]; } } int lca(int u,int v){ for(;; v = par[head[v]]){ if(in[u] > in[v]) swap(u,v); if(head[u] == head[v]) return u; } } template< typename T, typename Q, typename F > T query(int u, int v, const T &e, const Q &q, const F &f, bool edge = false){ T l = e, r = e; for(;;){ if(head[u] == head[v]) break; if(in[u] > in[v]) { l = f(l,q(in[head[u]],in[u] + 1)); u = par[head[u]]; } r = f(q(in[head[v]], in[v] + 1),r); v = par[head[v]]; } return f(f(l,q(in[u] + edge,in[v] + 1)), r); } template< typename Q > void add(int u, int v, const Q &q, bool edge = false){ for(;; v = par[head[v]]){ if(in[u] > in[v]) swap(u,v); if(head[u] == head[v]) break; q(in[head[v]], in[v] + 1); } q(in[u] + edge, in[v] + 1); } }; struct UnionFind { vector< int > data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(int x, int y) { x = find(x), y = find(y); if(x == y) return (false); if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return (true); } int find(int k) { if(data[k] < 0) return (k); return (data[k] = find(data[k])); } int size(int k) { return (-data[find(k)]); } }; template< class T > struct Matrix { vector< vector< T > > A; Matrix() {} Matrix(size_t n, size_t m) : A(n, vector< T >(m, 0)) {} Matrix(size_t n) : A(n, vector< T >(n, 0)) {}; size_t height() const { return (A.size()); } size_t width() const { return (A[0].size()); } inline const vector< T > &operator[](int k) const { return (A.at(k)); } inline vector< T > &operator[](int k) { return (A.at(k)); } static Matrix I(size_t n) { Matrix mat(n); for(int i = 0; i < n; i++) mat[i][i] = 1; return (mat); } Matrix &operator+=(const Matrix &B) { size_t n = height(), m = width(); assert(n == B.height() && m == B.width()); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) (*this)[i][j] += B[i][j]; return (*this); } Matrix &operator-=(const Matrix &B) { size_t n = height(), m = width(); assert(n == B.height() && m == B.width()); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) (*this)[i][j] -= B[i][j]; return (*this); } Matrix &operator*=(const Matrix &B) { size_t n = height(), m = B.width(), p = width(); assert(p == B.height()); vector< vector< T > > C(n, vector< T >(m, 0)); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) for(int k = 0; k < p; k++) C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]); A.swap(C); return (*this); } Matrix &operator^=(long long k) { Matrix B = Matrix::I(height()); while(k > 0) { if(k & 1) B *= *this; *this *= *this; k >>= 1LL; } A.swap(B.A); 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 { return (Matrix(*this) *= B); } Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); } friend ostream &operator<<(ostream &os, Matrix &p) { size_t n = p.height(), m = p.width(); for(int i = 0; i < n; i++) { os << "["; for(int j = 0; j < m; j++) { os << p[i][j] << (j + 1 == m ? "]\n" : ","); } } return (os); } T determinant() { Matrix B(*this); assert(width() == height()); T ret = 1; for(int i = 0; i < width(); i++) { int idx = -1; for(int j = i; j < width(); j++) { if(B[j][i] != 0) idx = j; } if(idx == -1) return (0); if(i != idx) { ret *= -1; swap(B[i], B[idx]); } ret *= B[i][i]; T vv = B[i][i]; for(int j = 0; j < width(); j++) { B[i][j] /= vv; } for(int j = i + 1; j < width(); j++) { T a = B[j][i]; for(int k = 0; k < width(); k++) { B[j][k] -= B[i][k] * a; } } } return (ret); } }; const ll MOD=1e9+7; const int N=1100000; template class modint { using u64 = ll; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(((x % Modulus) + Modulus)%Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; #define mint modint mint inv[N],comb[N],prd[N],invprd[N]; void calc_inv(){ inv[1]=1; rep2(i,2,N-1){ inv[i]=inv[MOD%i]*(-MOD/i); } return; } void calc_product(){ prd[0]=prd[1]=1; invprd[0]=invprd[1]=1; rep2(i,2,N-1){ prd[i]=prd[i-1]*i; invprd[i]=inv[i]*invprd[i-1]; } return ; } mint cmb(int a,int b){ if(a ; ostream& operator<<(ostream& os, mint a){ os << a.a ; return os; } ostream& operator<<(ostream& os, Matrix a){ rep(i,2)rep(j,2) cout << a[i][j] << " ";cout< edges; rep(i,n-1){ int a=in(),b=in(); g[a].eb(b); g[b].eb(a); edges.eb(a,b); } int Q = in(); Matrix I(2); I = I.I(2); HLDecomposition hld(g); hld.build(); SegmentTree> seg(n,[](Matrix x,Matrix y){return x*y;},I); while(Q--){ char c;cin>>c; if(c=='x'){ int k = in(); Matrix m(2); rep(i,2)rep(j,2)m[i][j] = in(); if(hld.par[edges[k].first] == hld.par[edges[k].se]){ seg.update(hld.in[edges[k].fi],m); } else seg.update(hld.in[edges[k].se],m); } else{ int i = in(),j = in(); cout << hld.query(i,j,I,[&](int x,int y){return seg.query(x,y);},[](Matrix x,Matrix y){return x*y;},true)<