#pragma GCC optimize ("O3") #pragma GCC target ("avx") #include using namespace std; const int mod = 1e9 + 7; uint32_t inv; int r2; int reduce(uint64_t x) { uint64_t y = uint64_t(uint32_t(x) * inv) * mod; return int(x >> 32) + mod - int(y >> 32); } int transform(int n) { return reduce(int64_t(n) * r2); } int normalize(int n) { return n >= mod ? n - mod : n; } void init_montgomery_reduction() { inv = 1; for (int i = 0; i < 5; ++i) inv *= 2 - inv * uint32_t(mod); r2 = -uint64_t(mod) % mod; } void chadd(int &a, int b) { (a += b - mod) < 0 ? a += mod : a; } int modadd(int a, int b) { return (a += b - mod) < 0 ? a + mod : a; } int modmul(int a, int b) { return reduce(int64_t(a) * b); } struct HLDecomposition { vector> g; vector vid, head, heavy, parent; HLDecomposition(int n) : g(n), vid(n, -1), head(n), heavy(n, -1), parent(n) {} void add(int u, int v) { g[u].push_back(v); g[v].push_back(u); } void build() { dfs(0, -1); bfs(); } int dfs(int curr, int prev) { parent[curr] = prev; int sub = 1, max_sub = 0; for (int next : g[curr]) if (next != prev) { int sub_next = dfs(next, curr); sub += sub_next; if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next; } return sub; } void bfs() { int k = 0; queue q({ 0 }); while (!q.empty()) { int h = q.front(); q.pop(); for (int i = h; i != -1; i = heavy[i]) { vid[i] = k++; head[i] = h; for (int j : g[i]) if (j != parent[i] && j != heavy[i]) q.push(j); } } } void for_each(int u, int v, function f) { if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]); if (head[u] != head[v]) for_each(u, parent[head[v]], f); } }; const int N = 1 << 18; struct node { int w, sum, add; }; node seg[N * 2]; void push(int k) { chadd(seg[k].sum, modmul(seg[k].add, seg[k].w)); chadd(seg[k * 2 + 0].add, seg[k].add); chadd(seg[k * 2 + 1].add, seg[k].add); seg[k].add = 0; } void push_sup(int k) { for (int i = 18; i >= 1; i--) push(k >> i); } int get_val(int k) { return modadd(seg[k].sum, modmul(seg[k].add, seg[k].w)); } void fix_sup(int k) { for (int i = 1; i <= 18; i++) { seg[k >> i].sum = modadd(get_val(k >> (i - 1)), get_val(k >> (i - 1) ^ 1)); } } void update(int l, int r, int v) { int ll = l; int rr = r; for (l += N, r += N; l < r; l >>= 1, r >>= 1) { if (l & 1) chadd(seg[l++].add, v); if (r & 1) chadd(seg[--r].add, v); } fix_sup(ll + N); fix_sup(rr + N - 1); } int query(int l, int r) { push_sup(l + N); push_sup(r + N - 1); int res = 0; for (l += N, r += N; l < r; l >>= 1, r >>= 1) { if (l & 1) chadd(res, get_val(l++)); if (r & 1) chadd(res, get_val(--r)); } return res; } int in() { int t; scanf("%d", &t); return t; } int in_t() { return transform(in()); } int main() { init_montgomery_reduction(); int n = in(); vector S(n), C(n); for (int i = 0; i < n; i++) S[i] = in_t(); for (int i = 0; i < n; i++) C[i] = in_t(); HLDecomposition hld(n); for (int i = 1; i < n; i++) hld.add(in() - 1, in() - 1); hld.build(); for (int i = 0; i < n; i++) { seg[hld.vid[i] + N].sum = S[i]; seg[hld.vid[i] + N].w = C[i]; } for (int i = N - 1; i >= 1; i--) { seg[i].sum = modadd(seg[i * 2 + 0].sum, seg[i * 2 + 1].sum); seg[i].w = modadd(seg[i * 2 + 0].w, seg[i * 2 + 1].w); } int q = in(); while (q--) { int t = in(); if (t == 0) { int u = in() - 1, v = in() - 1, z = in_t(); hld.for_each(u, v, [&](int l, int r) { update(l, r + 1, z); }); } else { int u = in() - 1, v = in() - 1; int ans = 0; hld.for_each(u, v, [&](int l, int r) { chadd(ans, query(l, r + 1)); }); printf("%d\n", normalize(reduce(ans))); } } }