#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r,v) auto r = (v) #else #define aut(r,v) __typeof(v) r = (v) #endif #define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it) #define all(o) (o).begin(), (o).end() #define pb(x) push_back(x) #define mp(x,y) make_pair((x),(y)) #define mset(m,v) memset(m,v,sizeof(m)) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL using namespace std; typedef vector vi; typedef pair pii; typedef vector > vpii; typedef long long ll; template inline void amin(T &x, U y) { if(y < x) x = y; } template inline void amax(T &x, U y) { if(x < y) x = y; } struct CentroidPathDecomposition { vector colors, positions; //Vertex -> Color, Vertex -> Offset vector lengths, parents, branches; //Color -> Int, Color -> Color, Color -> Offset vector parentnodes, depths; //Vertex -> Vertex, Vertex -> Int //vectorとかを避けて1次元にしたい時に使う //sortednodesの[lefts[v], rights[v])はvのsubtreeとなっている vector sortednodes, offsets; //Index -> Vertex, Color -> Index vector lefts, rights; //Vertex -> Index struct BuildDFSState { int i, len, parent; BuildDFSState() { } BuildDFSState(int i_, int l, int p): i(i_), len(l), parent(p) { } }; //両方の辺があってもいいし、親から子への辺だけでもよい void build(const vector &g, int root) { int n = g.size(); colors.assign(n, -1); positions.assign(n, -1); lengths.clear(); parents.clear(); branches.clear(); parentnodes.assign(n, -1); depths.assign(n, -1); sortednodes.clear(); offsets.clear(); lefts.assign(n, -1); rights.assign(n, -1); vector subtreesizes; measure(g, root, subtreesizes); typedef BuildDFSState State; depths[root] = 0; vector s; s.push_back(State(root, 0, -1)); while(!s.empty()) { State t = s.back(); s.pop_back(); int i = t.i, len = t.len; int index = sortednodes.size(); int color = lengths.size(); if(t.parent == -3) { rights[i] = index; continue; } if(t.parent != -2) { assert(parents.size() == color); parents.push_back(t.parent); branches.push_back(len); offsets.push_back(index); len = 0; } colors[i] = color; positions[i] = len; lefts[i] = index; sortednodes.push_back(i); int maxsize = -1, maxj = -1; each(j, g[i]) if(colors[*j] == -1) { if(maxsize < subtreesizes[*j]) { maxsize = subtreesizes[*j]; maxj = *j; } parentnodes[*j] = i; depths[*j] = depths[i] + 1; } s.push_back(State(i, -1, -3)); if(maxj == -1) { lengths.push_back(len + 1); }else { each(j, g[i]) if(colors[*j] == -1 && *j != maxj) s.push_back(State(*j, len, color)); s.push_back(State(maxj, len + 1, -2)); } } } void get(int v, int &c, int &p) const { c = colors[v]; p = positions[v]; } bool go_up(int &c, int &p) const { p = branches[c]; c = parents[c]; return c != -1; } inline const int *nodesBegin(int c) const { return &sortednodes[0] + offsets[c]; } inline const int *nodesEnd(int c) const { return &sortednodes[0] + (c+1 == offsets.size() ? sortednodes.size() : offsets[c+1]); } private: void measure(const vector &g, int root, vector &out_subtreesizes) const { out_subtreesizes.assign(g.size(), -1); vector s; s.push_back(root); while(!s.empty()) { int i = s.back(); s.pop_back(); if(out_subtreesizes[i] == -2) { int s = 1; each(j, g[i]) if(out_subtreesizes[*j] != -2) s += out_subtreesizes[*j]; out_subtreesizes[i] = s; }else { s.push_back(i); each(j, g[i]) if(out_subtreesizes[*j] == -1) s.push_back(*j); out_subtreesizes[i] = -2; } } } }; template struct ModInt { static const int Mod = MOD; unsigned x; ModInt(): x(0) { } ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; } ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { signed a = x, b = MOD, u = 1, v = 0; while(b) { signed t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } if(u < 0) u += Mod; ModInt res; res.x = (unsigned)u; return res; } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; typedef ModInt<1000000007> mint; vector coefs, coefsum; struct Val { mint val; mint coef; Val() { } }; struct Sum { mint sum, coefsum; Sum() { } Sum(const Val &val): sum(val.val), coefsum(val.coef) { } Sum &operator+=(const Sum &that) { sum += that.sum; coefsum += that.coefsum; return *this; } Sum operator+(const Sum &that) const { return Sum(*this) += that; } }; struct Add { mint add; Add() { } Add &operator+=(const Add &that) { add += that.add; return *this; } void addToVal(Val &val) const { val.val += add * val.coef; } void addToSum(Sum &sum) const { sum.sum += add * sum.coefsum; } }; struct Node { Node *parent, *left, *right; Val val; Sum sum; Add add; bool rev; Node(): parent(NULL), left(NULL), right(NULL), val(), sum(), add(), rev(false) { update(); } bool is_root() const { return !parent || (parent->left != this && parent->right != this); } void update() { sum = (!left ? Sum() : left->sum) + Sum(val) + (!right ? Sum() : right->sum); } void propagate() { if(rev) { if(left) left->rev ^= 1; if(right) right->rev ^= 1; swap(left, right); rev = false; } add.addToVal(val); add.addToSum(sum); if(left) left->add += add; if(right) right->add += add; add = Add(); } void rotateR() { propagate(); Node *q = parent, *r = q->parent; if(q->left = right) right->parent = q; right = q; q->parent = this; if(parent = r) { if(r->left == q) r->left = this; if(r->right == q) r->right = this; } q->update(); update(); } void rotateL() { propagate(); Node *q = parent, *r = q->parent; if(q->right = left) left->parent = q; left = q; q->parent = this; if(parent = r) { if(r->left == q) r->left = this; if(r->right == q) r->right = this; } q->update(); update(); } void topdown() { static vector route; for(Node *q = this; ; q = q->parent) { route.push_back(q); if(q->is_root()) break; } for(int i = route.size()-1; i >= 0; i --) { Node *q = route[i]; q->propagate(); } route.clear(); } void splay() { topdown(); while(!is_root()) { Node *q = parent; if(q->is_root()) { if(q->left == this) rotateR(); else rotateL(); }else { Node *r = q->parent; if(r->left == q) { if(q->left == this) q->rotateR(), rotateR(); else rotateL(), rotateR(); }else { if(q->right == this) q->rotateL(), rotateL(); else rotateR(), rotateL(); } } } } static Node *pathHead(Node *a) { Node *h = a; while(1) { h->propagate(); Node *c = h->left; if(!c) break; h = c; } h->splay(); return h; } static void splitPath(Node *a) { assert(a->is_root()); a->propagate(); Node *r = a->right; if(r != NULL) { a->right = NULL; a->update(); } } static void expose(Node *x) { Node *rp = NULL; for(Node *p = x; p; p = p->parent) { p->splay(); p->right = rp; p->update(); rp = p; } x->splay(); } static Node *exposePath(Node *a, Node *b) { evert(a); a->propagate(); Node *orgRoot = pathHead(a); expose(b); assert(a == b || a->parent != NULL); splitPath(b); return orgRoot; } static void evert(Node *x) { expose(x); splitPath(x); x->rev ^= true; } static void cut(Node *c) { expose(c); Node *p = c->left; assert(p); c->left = NULL; c->update(); p->parent = NULL; } static void connect(Node *x, Node *y) { evert(x); x->parent = y; } }; int main() { int N; while(~scanf("%d", &N)) { vector S(N), C(N); rep(i, N) scanf("%d", &S[i]); rep(i, N) scanf("%d", &C[i]); vector nodes(N); rep(i, N) { nodes[i].val.val = S[i]; nodes[i].val.coef = C[i]; nodes[i].update(); } rep(i, N-1) { int A, B; scanf("%d%d", &A, &B), -- A, -- B; Node::connect(&nodes[A], &nodes[B]); } int Q; scanf("%d", &Q); rep(ii, Q) { int ty; scanf("%d", &ty); if(ty == 0) { int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z), -- X, -- Y; Add add; add.add = Z; Node::exposePath(&nodes[X], &nodes[Y]); nodes[Y].add += add; }else { int X, Y; scanf("%d%d", &X, &Y), -- X, -- Y; Node::exposePath(&nodes[X], &nodes[Y]); mint ans = nodes[Y].sum.sum; printf("%d\n", ans.get()); } } } return 0; }