#pragma GCC optimize ("O3") #pragma GCC target ("avx") #include #include #include const int mod = (int)1e9+7; const int MAX = 100000; int n; std::vector g[MAX]; int es[MAX-1][2]; int par[MAX]; long long mat[MAX][4]; const long long id[4] = {1,0,0,1,}; void dfs(int v, int p) { par[v] = p; memcpy(mat[v], id, sizeof(id)); for (int t : g[v]) if (t != p) dfs(t, v); } void merge(long long *a, long long *b) { long long t[] = { (a[0] * b[0] + a[1] * b[2]) % mod, (a[0] * b[1] + a[1] * b[3]) % mod, (a[2] * b[0] + a[3] * b[2]) % mod, (a[2] * b[1] + a[3] * b[3]) % mod, }; memcpy(b, t, sizeof(t)); } int main() { scanf("%d", &n); for (int i = 0; i < n - 1; i++) { int a, b; scanf("%d%d", &a, &b); es[i][0] = a; es[i][1] = b; g[a].push_back(b); g[b].push_back(a); } dfs(0, -1); for (int i = 0; i < n - 1; i++) { if (par[es[i][0]] == es[i][1]) { std::swap(es[i][0], es[i][1]); } } int q; scanf("%d", &q); while (q-- > 0) { char c[2]; scanf("%s", c); if (c[0] == 'x') { int j, x00, x01, x10, x11; scanf("%d%d%d%d%d", &j, &x00, &x01, &x10, &x11); const int v = es[j][1]; mat[v][0] = x00; mat[v][1] = x01; mat[v][2] = x10; mat[v][3] = x11; } else if (c[0] == 'g') { int i, j; scanf("%d%d", &i, &j); long long ans[4] = {1,0,0,1,}; for (int v = j; v != i; v = par[v]) { merge(mat[v], ans); } printf("%d %d %d %d\n", (int)ans[0], (int)ans[1], (int)ans[2], (int)ans[3]); } else { throw; } } }