#include using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; #define rep(i,n) for(ll i=0;i T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); } template T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); } template inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template ostream &operator<<(ostream &os, const vector &a){ if (a.empty()) return os; os << a.front(); for (auto e : a | views::drop(1)){ os << ' ' << e; } return os; } void dump(auto ...vs){ ((cout << vs << ' '), ...) << endl; } #include using namespace atcoder; using mint = modint998244353; void solve() { ll N; cin>>N; vector A(N); ll sa=0; rep(i,N){ cin>>A[i]; sa+=A[i]; } vector>> child(N); vector> parent(N); vector B(N); rep(i,N-1){ cin>>B[i+1]; B[i+1]--; } vector C(N); rep(i,N-1){ cin>>C[i+1]; C[i+1]--; } vector P(N); rep(i,N-1){ cin>>P[i+1]; P[i+1]--; } for (ll i=1;i S(N); auto dfs=[&](auto self,ll cp)->ll { ll v=A[cp]; for (auto [_,np]:child[cp]){ v+=self(self,np); } S[cp]=v; return v; }; dfs(dfs,0); mint ans=0; for (ll cp=1;cp> C; for (auto [a,np]:child[cp]){ C.emplace_back(a,S[np]); } if (cp!=0){ C.emplace_back(parent[cp].first,sa-S[cp]); } sort(all(C),[](pair a,pair b){ return (a.firstsync_with_stdio(0); ll T=1; while (T--){ solve(); } return 0; }