QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#7392. Minimax

统计

Little $C$ has a rooted tree with $n$ nodes, where the root is node $1$, and each node has at most two children.

The value of a node $x$ is defined as follows:

  1. If $x$ has no children, its value is given in the input. It is guaranteed that the values of all such nodes are distinct.

  2. If $x$ has children, its value is the maximum of its children's values with probability $p_x$, and the minimum of its children's values with probability $1-p_x$.

Now, Little $C$ wants to know the following: assuming the value of node $1$ has $m$ possible outcomes, where the $i$-th smallest possible value is $V_i$ with probability $D_i$ ($D_i > 0$), calculate:

$$\sum_{i=1}^{m}i\cdot V_i\cdot D_i^2$$

You need to output the answer modulo $998244353$.

Input

The first line contains a positive integer $n$.

The second line contains $n$ integers, where the $i$-th integer represents the parent of node $i$. The parent of node $1$ is $0$.

The third line contains $n$ integers. If node $i$ has no children, the $i$-th number is its value. Otherwise, the $i$-th number is $p_i \cdot 10000$. It is guaranteed that $p_i \cdot 10000$ is a positive integer.

Output

Output the answer.

Examples

Input 1

3
0 1 1
5000 1 2

Output 1

748683266

Note 1

The value of node $1$ is $1$ with probability $\frac{1}{2}$ and $2$ with probability $\frac{1}{2}$, so the answer is $\frac{5}{4}$.

Subtasks

For $10\%$ of the data, $1 \leq n \leq 20$.

For $20\%$ of the data, $1 \leq n \leq 400$.

For $40\%$ of the data, $1 \leq n \leq 5000$.

For $60\%$ of the data, $1 \leq n \leq 10^5$.

An additional $10\%$ of the data guarantees that the tree structure is random.

For $100\%$ of the data, $1 \leq n \leq 3 \times 10^5$ and $1 \leq w_i \leq 10^9$.

For all data, it is satisfied that $0 < p_i \cdot 10000 < 10000$, so it is easy to prove that the value of every leaf has a non-zero probability of being taken by the root.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.