#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]--; p.insert(p.begin(), -1); 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 + (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, 1, n) { g[i].push_back({p[i], b[i - 1], c[i - 1]}); g[p[i]].push_back({(int)i, c[i - 1], b[i - 1]}); } auto dfs = [&](auto self, int nw, int pr, int bb) -> pair { mint sz = 0, sm = 0; for (auto [nx, bd, cd] : g[nw]) { if (nx == pr) continue; auto [csz, csm] = self(self, nx, nw, cd); csm += csz; ans += csm * a[nw]; ans += f(a[nw], bd) * csz; sz += csz; sm += csm + abs(bd - bb) * csz; } 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(); }