LCA - 2#3299

LCA - 2

6 seconds
128 megabytes
Easy

You are given a rooted tree with n~(1 \le n \le 10^5) vertices, numbered from to n - 1. You need to process m~(1 \le m \le 10^7) queries asking for the Lowest Common Ancestor (LCA) of pairs of vertices.

The queries are generated as follows. The values a_1, a_2 and integers x, y, z are given. For i \ge 3, the values a_i are computed by the formula:

a_i = (x \cdot a_{i-2} + y \cdot a_{i-1} + z) \bmod n.

The first query is (a_1, a_2). Let the answer to the (i-1)-th query be . Then the -th query is defined as

((a_{2i-1} + v) \bmod n,\ a_{2i})

Input

The first line contains two integers and . The root of the tree is vertex .

The second line contains n - 1 integers: the -th of them is the parent of vertex i~(1 \le i \le n - 1).

The third line contains two integers a_1 and a_2~(0 \le a_1, a_2 < n).

The fourth line contains three integers and z~(0 \le x, y, z \le 10^9).

Output

Print the sum of the vertices that are answers to all queries.

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading