#include using namespace std; const long long MOD = 1e9 + 7; //************************************************************** // link-cut tree //************************************************************** struct node { node *parent = nullptr; node *lch = nullptr; node *rch = nullptr; bool reversed = false; long long value; long long sum; long long coef; long long sum_coef; long long lazy = 0; node(long long value, long long coef) { this->value = this->sum = value; this->coef = this->sum_coef = coef; } }; long long sum_coef(node *x) { return x == nullptr ? 0 : x->sum_coef; } long long sum(node *x) { return x == nullptr ? 0 : x->sum; } void update(node *x) { if (x == nullptr) return; x->sum_coef = (x->coef + sum_coef(x->lch) + sum_coef(x->rch)) % MOD; x->sum = (x->value + sum(x->lch) + sum(x->rch)) % MOD; } void push(node *x) { if (x == nullptr) return; for (node *y : { x->lch, x->rch }) if (y != nullptr) { y->reversed ^= x->reversed; (y->lazy += x->lazy) %= MOD; (y->sum += x->lazy * sum_coef(y)) %= MOD; } if (x->reversed) swap(x->lch, x->rch); x->reversed = false; (x->value += x->lazy * x->coef) %= MOD; x->lazy = 0; update(x); } //************************************************************** // link-cut tree basic operation //************************************************************** enum { LEFT, RIGHT }; bool is_root(node *x) { return x->parent == nullptr || (x->parent->lch != x && x->parent->rch != x); } int parent_direction(node *x) { if (is_root(x)) return -1; return x->parent->rch == x ? LEFT : RIGHT; } void connect(node *child, node *parent, int dir) { if (parent != nullptr && dir != -1) (dir == RIGHT ? parent->lch : parent->rch) = child; if (child != nullptr) child->parent = parent; } void raise(node *x) { node *y = x->parent; node *z = y->parent; int xdir = parent_direction(x); int ydir = parent_direction(y); connect(xdir == LEFT ? x->lch : x->rch, y, xdir); connect(y, x, !xdir); connect(x, z, ydir); update(y); update(x); update(z); } void splay(node *x) { while (!is_root(x)) { push(x->parent->parent); push(x->parent); push(x); if (is_root(x->parent)) { raise(x); } else if (parent_direction(x) == parent_direction(x->parent)) { raise(x->parent); raise(x); } else { raise(x); raise(x); } } push(x); } void expose(node *x) { for (node *y = x, *rch = nullptr; y != nullptr; y = y->parent) { splay(y); y->rch = rch; rch = y; update(y); } splay(x); } void cut(node *x) { expose(x); x->lch->parent = nullptr; x->lch = nullptr; update(x); } void link(node *x, node *p) { expose(x); expose(p); x->parent = p; p->rch = x; update(x); update(p); } void evert(node *x) { expose(x); x->rch = x->lch; x->lch = nullptr; if (x->rch != nullptr) x->rch->reversed ^= 1; update(x); } //************************************************************** // main //************************************************************** vector g[202020]; node *tr[202020]; void dfs(int curr, int prev) { for (int next : g[curr]) if (next != prev) { link(tr[next], tr[curr]); dfs(next, curr); } } int main() { int n; cin >> n; vector S(n), C(n); for (int i = 0; i < n; i++) scanf("%d", &S[i]); for (int i = 0; i < n; i++) scanf("%d", &C[i]); for (int i = 0; i < n; i++) tr[i] = new node(S[i], C[i]); for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; g[u].push_back(v); g[v].push_back(u); } dfs(0, -1); int q; cin >> q; for (int i = 0; i < q; i++) { int k; scanf("%d", &k); if (k == 0) { int x, y, z; scanf("%d %d %d", &x, &y, &z); x--; y--; evert(tr[x]); expose(tr[y]); node *rch = tr[y]->rch; tr[y]->rch = nullptr; tr[y]->lazy = z; push(tr[y]); tr[y]->rch = rch; update(tr[y]); } else { int x, y; scanf("%d %d", &x, &y); x--; y--; evert(tr[x]); expose(tr[y]); node *rch = tr[y]->rch; tr[y]->rch = nullptr; update(tr[y]); long long ans = sum(tr[y]); tr[y]->rch = rch; update(tr[y]); printf("%lld\n", ans); } } }