#include using namespace std; #define rep(i,n) for (int i = 0; i< (n); ++i) #define repi(i, a, b) for (int i = (a); i < (b); ++i) #define all(x) (x).begin(), (x).end() #define fore(i, a) for(auto &i:a) using ll = long long; using int64 = long long; #define DEBUG(x) cerr << #x << ": "; for (auto _ : x) cerr << _ << " "; cerr << endl; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template ostream& operator<<(ostream& os, const pair& p) { os << p.first << " " << p.second; return os; } template istream& operator>>(istream& is, pair& p) { is >> p.first >> p.second; return is; } template ostream& operator<<(ostream& os, const vector& v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template istream& operator>>(istream& is, vector& v) { for (T& in : v) is >> in; return is; } template inline bool chmax(T1& a, T2 b) { return a < b && (a = b, true); } template inline bool chmin(T1& a, T2 b) { return a > b && (a = b, true); } template vector make_v(size_t a) { return vector(a); } template auto make_v(size_t a, Ts... ts) { return vector(ts...))>(a, make_v(ts...)); } template typename enable_if::value == 0>::type fill_v(T& t, const V& v) { t = v; } template typename enable_if::value != 0>::type fill_v(T& t, const V& v) { for (auto& e : t) fill_v(e, v); } template struct FixPoint : F { explicit FixPoint(F&& f) : F(std::forward(f)) {} template decltype(auto) operator()(Args&&... args) const { return F::operator()(*this, std::forward(args)...); } }; template inline decltype(auto) MFP(F&& f) { return FixPoint{std::forward(f)}; } template struct Edge { int from, to; T cost; int idx; Edge() = default; Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } }; template struct Graph { vector > > g; int es; Graph() = default; explicit Graph(int n) : g(n), es(0) {} size_t size() const { return g.size(); } void add_directed_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es++); } void add_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es); g[to].emplace_back(to, from, cost, es++); } void read(int M, int padding = -1, bool weighted = false, bool directed = false) { for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a += padding; b += padding; T c = T(1); if (weighted) cin >> c; if (directed) add_directed_edge(a, b, c); else add_edge(a, b, c); } } inline vector >& operator[](const int& k) { return g[k]; } inline const vector >& operator[](const int& k) const { return g[k]; } }; template using Edges = vector >; //depend on graph-template template struct HeavyLightDecomposition : Graph { public: using Graph::Graph; using Graph::g; vector sz, in, out, head, rev, par, dep; void build(int root = 0) { sz.assign(g.size(), 0); in.assign(g.size(), 0); out.assign(g.size(), 0); head.assign(g.size(), 0); rev.assign(g.size(), 0); par.assign(g.size(), 0); dep.assign(g.size(), 0); dfs_sz(root, -1, 0); int t = 0; head[root] = root; dfs_hld(root, -1, t); } /* k: 0-indexed */ 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) const { for (;; v = par[head[v]]) { if (in[u] > in[v]) swap(u, v); if (head[u] == head[v]) return u; } } int dist(int u, int v) const { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } template E query(int u, int v, const E& ti, const Q& q, const F& f, const S& s, bool edge = false) { E l = ti, r = ti; for (;; v = par[head[v]]) { if (in[u] > in[v]) swap(u, v), swap(l, r); if (head[u] == head[v]) break; l = f(q(in[head[v]], in[v] + 1), l); } return s(f(q(in[u] + edge, in[v] + 1), l), r); } template E query(int u, int v, const E& ti, const Q& q, const F& f, bool edge = false) { return query(u, v, ti, q, f, f, edge); } template 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); } /* {parent, child} */ vector > compress(vector& remark) { auto cmp = [&](int a, int b) { return in[a] < in[b]; }; sort(begin(remark), end(remark), cmp); remark.erase(unique(begin(remark), end(remark)), end(remark)); int K = (int)remark.size(); for (int k = 1; k < K; k++) remark.emplace_back(lca(remark[k - 1], remark[k])); sort(begin(remark), end(remark), cmp); remark.erase(unique(begin(remark), end(remark)), end(remark)); vector > es; stack st; for (auto& k : remark) { while (!st.empty() && out[st.top()] <= in[k]) st.pop(); if (!st.empty()) es.emplace_back(st.top(), k); st.emplace(k); } return es; } explicit HeavyLightDecomposition(const Graph& g) : Graph(g) {} private: void dfs_sz(int idx, int p, int d) { dep[idx] = d; par[idx] = p; sz[idx] = 1; if (g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back()); for (auto& to : g[idx]) { if (to == p) continue; dfs_sz(to, idx, d + 1); sz[idx] += sz[to]; if (sz[g[idx][0]] < sz[to]) swap(g[idx][0], to); } } void dfs_hld(int idx, int p, int& times) { in[idx] = times++; rev[in[idx]] = idx; for (auto& to : g[idx]) { if (to == p) continue; head[to] = (g[idx][0] == to ? head[idx] : to); dfs_hld(to, idx, times); } out[idx] = times; } }; #include #include using namespace atcoder; using mint = modint1000000007; struct S{ mint x00, x01, x10, x11; }; S op(S a, S b){ S n; n.x00 = a.x00*b.x00+a.x01*b.x10; n.x01 = a.x00*b.x01+a.x01*b.x11; n.x10 = a.x10*b.x00+a.x11*b.x10; n.x11 = a.x10*b.x01+a.x11*b.x11; return n; } S e(){ S E={1,0,0,1}; return E; } struct F{ bool flag; mint x00, x01, x10, x11; }; S mapping(F f, S x){ if(f.flag){ return S{f.x00,f.x01,f.x10,f.x11}; } else{ return x; } } F composition(F f, F g){ if(g.flag){ return g; } else{ return f; } } F id(){ return F{false, 0,0,0,0}; } int main(){ ll n; cin >> n; Graph G(n); vector a(n-1),b(n-1); rep(i, n-1){ int aa, bb; cin >> aa >> bb; a[i] = aa; b[i] = bb; G.add_edge(aa,bb); } HeavyLightDecomposition g(G); g.build(); ll q;cin>>q; lazy_segtree seg(n); rep(qq, q){ char t; cin >> t; if(t == 'x'){ ll i, ee, f, c, d; cin >> i >> ee >> f >> c >> d; auto Q = [&](ll l, ll r){ seg.apply(l, r, F{true, ee, f, c, d}); }; g.add(a[i],b[i] , Q,true); } else{ ll i, j; cin >> i >> j; auto R = [&](ll l, ll r){ return seg.prod(l, r); }; S res = g.query(i, j, e(), R, op, true); cout << res.x00.val() << " " << res.x01.val() << " " << res.x10.val() << " " << res.x11.val() << "\n"; } } }