#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; using mint = atcoder::modint998244353; void solve() { int n; cin >> n; vector a(n), b(n - 1), c(n - 1), p(n - 1); rep(i, 0, n) cin >> a[i]; rep(i, 0, n - 1) cin >> b[i], b[i]--; rep(i, 0, n - 1) cin >> c[i], c[i]--; rep(i, 0, n - 1) cin >> p[i], p[i]--; auto sum1 = [](ll x) { return mint(x + 1) * x / 2; }; auto sum2 = [](ll x) { return mint(x + 1) * x * (2 * x + 1) / 6; }; auto f = [&](ll x, ll y) { return sum1(x - 1) - sum2(y) * 2 + mint(2 + 2 * y - x) * y; }; mint ans = 0; rep(i, 0, n) { ans += a[i] * sum1(a[i]) - sum2(a[i]); } vector>> g(n); rep(i, 0, n - 1) { g[i + 1].push_back({b[i], p[i], c[i]}); g[p[i]].push_back({c[i], (int)i + 1, b[i]}); } rep(i, 0, n) sort(g[i].begin(), g[i].end()); auto dfs = [&](auto self, int nw, int pr, int bb) -> pair { mint sz = 0, sm = 0; mint tmp_sm = 0; int tmp_d = -1; for (auto [bd, nx, cd] : g[nw]) { if (nx == pr) continue; auto [csz, csm] = self(self, nx, nw, cd); csm += csz; tmp_sm += (bd - tmp_d) * sz; tmp_d = bd; ans += tmp_sm * csz; ans += sz * csm; ans += csm * a[nw]; ans += f(a[nw], bd) * csz; sz += csz; sm += csm + abs(bd - bb) * csz; tmp_sm += csm; } sz += a[nw]; sm += f(a[nw], bb); return {sz, sm}; }; dfs(dfs, 0, -1, 0); ans *= 2; cout << ans.val() << '\n'; } int main() { int t = 1; // cin >> t; while (t--) solve(); }