1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include <bits/stdc++.h> using namespace std;
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
template <typename T,typename... Args> inline void r1(T& t, Args&... args) { r1(t); r1(args...); }
const int maxn = 3e5 + 5; const int maxm = maxn << 1; const int mod = 998244353; int ksm(int x,int mi) { int res(1); while(mi) { if(mi & 1) res = 1ll * res * x % mod; mi >>= 1; x = 1ll * x * x % mod; } return res; } const int Inv = ksm(10000, mod - 2); int n; vector<int> vc[maxn]; int val[maxn], p[maxn]; int c[maxn], ed(0), len; int ansp[maxn];
int t[maxn * 20], tot(0); int lsn[maxn * 20], rsn[maxn * 20], tag[maxn * 20];
struct Seg { #define ls lsn[p] #define rs rsn[p] #define mid ((l + r) >> 1)
void pushup(int p) { t[p] = (t[ls] + t[rs]) % mod; }
void Add(int p,int c) { if(p == 0) return ; t[p] = 1ll * t[p] * c % mod; tag[p] = 1ll * tag[p] * c % mod; }
void pushdown(int p) { if(tag[p] == 1) return ; Add(ls, tag[p]), Add(rs, tag[p]); tag[p] = 1; }
void Insert(int &p,int l,int r,int pos,int c) { if(!p) p = ++ tot, tag[p] = 1; if(l == r) return t[p] = c, void(); if(pos <= mid) Insert(ls, l, mid, pos, c); else Insert(rs, mid + 1, r, pos, c); pushup(p); }
int merge(int u,int v,int l,int r,int mulu,int mulv,const int &P) {
if(!u) return Add(v, mulv), v; if(!v) return Add(u, mulu), u; pushdown(u), pushdown(v); int vul = t[lsn[u]], vur = t[rsn[u]], vvl = t[lsn[v]], vvr = t[rsn[v]]; const int P1 = (1 - P + mod) % mod; lsn[u] = merge(lsn[u], lsn[v], l, mid, (mulu + 1ll * P1 * vvr % mod) % mod, (mulv + 1ll * P1 * vur % mod) % mod, P); rsn[u] = merge(rsn[u], rsn[v], mid + 1, r, (mulu + 1ll * P * vvl % mod) % mod, (mulv + 1ll * P * vul % mod) % mod, P); pushup(u); return u; }
}T;
int rt[maxn];
void dfs(int p) { if(vc[p].size() == 0) return T.Insert(rt[p], 1, len, val[p], 1); for(int v : vc[p]) dfs(v);
if(vc[p].size() == 1) return rt[p] = rt[vc[p][0]], void(); if(vc[p].size() == 2) return rt[p] = T.merge(rt[vc[p][0]], rt[vc[p][1]], 1, len, 0, 0, ::p[p]), void(); }
void getans(int p,int l,int r) { if(!p) return ; if(l == r) return ansp[l] = t[p], void(); T.pushdown(p); getans(ls, l, mid), getans(rs, mid + 1, r); }
#undef ls #undef rs #undef mid
signed main() {
int i, j; r1(n); for(i = 1; i <= n; ++ i) { int x; r1(x); if(i == 1) continue; vc[x].push_back(i); } for(i = 1; i <= n; ++ i) { int x; r1(x);
if(vc[i].size() == 0) val[i] = x, c[++ ed] = x; else p[i] = 1ll * x * Inv % mod; }
sort(c + 1, c + ed + 1); len = unique(c + 1, c + ed + 1) - c - 1; for(i = 1; i <= n; ++ i) if(p[i] == 0) val[i] = lower_bound(c + 1, c + len + 1, val[i]) - c;
dfs(1);
getans(rt[1], 1, len);
int ans(0); for(i = 1; i <= len; ++ i) ans = (ans + 1ll * i * c[i] % mod * ansp[i] % mod * ansp[i] % mod) % mod; printf("%d\n", ans); return 0; }
|