#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } #include #include using namespace atcoder; using mint = modint1000000007; struct S { mint x00, x01, x10, x11; }; S op(S a, S b) { return S{a.x00 * b.x00 + a.x01 * b.x10, a.x00 * b.x01 + a.x01 * b.x11, a.x10 * b.x00 + a.x11 * b.x10, a.x10 * b.x01 + a.x11 * b.x11}; } S e() { return S{1, 0, 0, 1}; } int main() { fast_io(); int n; cin >> n; vector> g(n); vector> idx(n - 1); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); idx[i] = {a, b}; } vector sz(n), par(n); { auto dfs = [&](auto self, int u, int p) -> void { sz[u] = 1; for (int v : g[u]) { if (v == p) continue; par[v] = u; self(self, v, u); sz[u] += sz[v]; } }; dfs(dfs, 0, -1); vector> g_new(n); for (int i = 0; i < n; i++) { for (int j : g[i]) { if (j != par[i]) { g_new[i].push_back(j); } } sort(g_new[i].begin(), g_new[i].end(), [&](int a, int b) { return sz[a] > sz[b]; }); } g = g_new; } vector dfs_ord(n), root(n); { iota(root.begin(), root.end(), 0); vector ord; auto dfs = [&](auto self, int u) -> void { dfs_ord[u] = ord.size(); ord.push_back(u); for (int v : g[u]) { if (v == g[u][0]) { root[v] = root[u]; } self(self, v); } }; dfs(dfs, 0); } for (int i = 0; i < n - 1; i++) { if (idx[i].first != par[idx[i].second]) { swap(idx[i].first, idx[i].second); } } vector seg_data(n, e()); segtree seg(seg_data); int q; cin >> q; for (; q--;) { char com; cin >> com; if (com == 'x') { int i, x00, x01, x10, x11; cin >> i >> x00 >> x01 >> x10 >> x11; int j = dfs_ord[idx[i].second]; seg.set(j, S{x00, x01, x10, x11}); } if (com == 'g') { int i, j; cin >> i >> j; S ans = e(); while (i != j) { if (root[j] == j) { ans = op(seg.get(dfs_ord[j]), ans); j = par[j]; } else if (root[j] != root[i]) { ans = op(seg.prod(dfs_ord[root[j]], dfs_ord[j] + 1), ans); j = par[root[j]]; } else { ans = op(seg.prod(dfs_ord[i] + 1, dfs_ord[j] + 1), ans); break; } } cout << ans.x00.val() << " " << ans.x01.val() << " " << ans.x10.val() << " " << ans.x11.val() << "\n"; } } }