F. Mirror II
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tree of size $$$n$$$ rooted at node $$$1$$$. Each node $$$u$$$ has a value $$$val_u$$$.

Let $$$dis(u, v)$$$ be the sum of the values of the nodes in the simple path from $$$u$$$ to $$$v$$$. Also, let $$$d_u = dis(1, u)$$$. Two nodes $$$u$$$ and $$$v$$$ are considered similar if $$$(d_u + d_v)^2 = (d_u - d_v)^2 = dis(u, v)$$$.

Your goal is to find the number of pairs $$$(u,v)$$$, $$$1 \le u \lt v \le n$$$, such that nodes $$$u$$$ and $$$v$$$ are similar.

Input

The first line of the input contains one integer, $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$.

The second line of the input contains $$$n$$$ integers $$$val_1, val_2, ..., val_n$$$ $$$(-10^9 \le val_i \le 10^9)$$$, where $$$val_i$$$ is the value of the $$$i$$$-$$$th$$$ node.

The third line of the input contains $$$n-1$$$ integers $$$p_2, p_3, ..., p_n$$$ , where $$$p_i$$$ is the parent of the $$$i$$$-$$$th$$$ node.

It is guaranteed that the input forms a valid tree.

Output

Output exactly one integer, the number of pairs $$$(u,v)$$$ $$$(1 \le u \lt v \le n)$$$ such that nodes $$$u$$$ and $$$v$$$ are similar.

Examples
Input
6
0 1 0 -1 0 0
1 2 2 4 1
Output
9
Input
10
0 -7 0 5 7 0 -1 0 7 0
9 2 1 8 2 5 1 8 3
Output
10
Note

For the first example:

The pairs are $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(1, 5)$$$, $$$(1, 6)$$$, $$$(2, 6)$$$, $$$(3, 6)$$$, $$$(4, 6)$$$, $$$(5, 6)$$$.

Take as an example the pair $$$(4, 6)$$$. Because $$$d_4 = 0$$$, $$$d_6 = 0$$$, and $$$dis(4, 6) = 0$$$, then $$$(d_4 + d_6)^2 = (d_4 - d_6)^2 = dis(4, 6) = 0$$$. Therefore, the nodes $$$4$$$ and $$$6$$$ are similar.