#include using namespace std::literals::string_literals; using i64 = long long; using std::cout; using std::endl; using std::cin; template std::vector make_v(size_t a){return std::vector(a);} template auto make_v(size_t a,Ts... ts){ return std::vector(ts...))>(a,make_v(ts...)); } template class lazy_segment_tree { using value_type = Monoid; using operator_type = OperatorMonoid; using size_type = size_t; using F = std::function; using G = std::function; using H = std::function; size_type size_; size_type height_; F f; G g; H h; value_type id; operator_type id_op; std::vector data; std::vector lazy; std::vector depth; const size_type get_height(const size_type& size) const { size_type height = 1; while(1 << height < size) height++; return height; } const size_type base_size() const { return 1 << height_; } void thrust(const size_type & k) { for(int i = height_; i; i--) eval(k >> i); } void eval(const size_type & k) { if(lazy[k] == id_op) return; lazy[k << 1 ^ 0] = h(lazy[k << 1 ^ 0], lazy[k]); lazy[k << 1 ^ 1] = h(lazy[k << 1 ^ 1], lazy[k]); data[k] = reflect(k); lazy[k] = id_op; } const value_type reflect(const size_type & k) { if(lazy[k] == id_op) return data[k]; int l = k % (1 << depth[k]) * (base_size() >> depth[k]); int r = l + (base_size() >> depth[k]); return g(data[k], lazy[k], l, r); } void recalc(size_type k) { while(k >>= 1) data[k] = f(reflect(k << 1 ^ 0), reflect(k << 1 ^ 1)); } public: lazy_segment_tree() {} lazy_segment_tree(int n, const F & f, const G & g, const H & h, const value_type & id, const operator_type & id_op) : size_(n), f(f), g(g), h(h), id(id), id_op(id_op) { height_ = get_height(size_); data.assign(base_size() << 1, id); lazy.assign(base_size() << 1, id_op); depth.assign(base_size() << 1, 0); for(int i = 0; i < height_ + 1; i++) for(int j = (1 << i); j < (1 << (i + 1)); j++) depth[j] = i; } void update(size_type a, size_type b, operator_type x) { thrust(a += base_size()); thrust(b += base_size() - 1); for(size_type l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if(l & 1) lazy[l] = h(lazy[l++], x); if(r & 1) lazy[--r] = h(lazy[r], x); } recalc(a); recalc(b); } void change(size_type k, const value_type x) { thrust(k += base_size()); data[k] = x; lazy[k] = id_op; recalc(k); } const value_type fold(size_type a, size_type b) { thrust(a += base_size()); thrust(b += base_size() - 1); value_type left_value = id; value_type right_value = id; for(size_type l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if(l & 1) left_value = f(left_value, reflect(l++)); if(r & 1) right_value = f(right_value, reflect(--r)); } return f(left_value, right_value); } const value_type operator[](const size_type & k) { return fold(k, k + 1); } }; // build: you have to run it before you use // for_each: process [l, r] // for_each_edge: process [l, r] // distance: returns the dist (l, r) class heavy_light_decomposition { using size_type = size_t; using F = std::function; public: std::vector> g; std::vector vid, inv; private: std::vector head, sz, heavy, par, dep, type; void dfs(int root) { using P = std::pair; par[root] = -1; dep[root] = 0; std::stack

st; st.push({root, 0}); while(!st.empty()) { int v = st.top().first; int & i = st.top().second; if(i < g[v].size()) { int u = g[v][i++]; if(u == par[v]) continue; par[u] = v; dep[u] = dep[v] + 1; st.push({u, 0}); } else { st.pop(); int tmp = 0; for(auto e: g[v]) { if(e == par[v]) continue; sz[v] += sz[e]; if(tmp < sz[e]) { tmp = sz[e]; heavy[v] = e; } } } } } void bfs(int root, int c, int & k) { std::queue qu({root}); while(!qu.empty()) { int h = qu.front(); qu.pop(); for(int v = h; v != -1; v = heavy[v]) { type[v] = c; vid[v] = k++; inv[vid[v]] = v; head[v] = h; for(int e: g[v]) if(e != par[v] and e != heavy[v]) qu.push(e); } } } public: heavy_light_decomposition() {} heavy_light_decomposition(int n) : g(n), vid(n, -1), head(n), sz(n, 1), heavy(n, -1), par(n), dep(n), inv(n), type(n) {} void add_edge(int a, int b) { g[a].push_back(b); g[b].push_back(a); } void build(std::vector rs = std::vector(1, 0)) { int c = 0, k = 0; for(int r: rs) { dfs(r); bfs(r, c++, k); } } int lca(int u, int v) { while(true) { if(vid[u] > vid[v]) std::swap(u, v); if(head[u] == head[v]) return u; v = par[head[v]]; } } void for_each(int u, int v, const F & f) { while(true) { if(vid[u] > vid[v]) std::swap(u, v); f(std::max(vid[head[v]], vid[u]), vid[v]); if(head[u] != head[v]) v = par[head[v]]; else break; } } void for_each_edge(int u, int v, const F & f) { while(true) { if(vid[u] > vid[v]) std::swap(u, v); if(head[u] != head[v]) { f(vid[head[v]], vid[v]); v = par[head[v]]; } else { if(u != v) f(vid[u] + 1, vid[v]); break; } } } int distance(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } }; int main() { int n; scanf("%d", &n); std::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]); heavy_light_decomposition hld(n); for(int i = 0; i < n - 1; i++) { int a, b; scanf("%d%d", &a, &b); hld.add_edge(a - 1, b - 1); } hld.build(); std::vector S(n + 1); for(int i = 0; i < n; i++) S[i + 1] = S[i] + c[hld.inv[i]]; const int MOD = 1e9 + 7; auto f = [MOD](i64 a, i64 b) { return (a + b) % MOD; }; auto g = [MOD, S](i64 a, i64 b, int l, int r) { return ((S[r] - S[l]) % MOD * b % MOD + a) % MOD; }; auto h = [MOD](i64 a, i64 b) { return (a + b) % MOD; }; lazy_segment_tree seg(n, f, g, h, 0, 0); for(int i = 0; i < n; i++) seg.change(i, s[hld.inv[i]]); int q; scanf("%d", &q); while(q--) { int type; scanf("%d", &type); if(type == 0) { int x, y, z; scanf("%d%d%d", &x, &y, &z); auto f = [&seg, z](int l, int r) { seg.update(l, r + 1, z); }; hld.for_each(x - 1, y - 1, f); } else { int x, y; scanf("%d%d", &x, &y); i64 ans = 0; auto f = [&seg, &ans, &MOD](int l, int r) { (ans += seg.fold(l, r + 1)) %= MOD; }; hld.for_each(x - 1, y - 1, f); printf("%lld\n", ans); } } return 0; }