LCA Problem Revisited (Easy)#2318

LCA Problem Revisited (Easy)

1 second
64 megabytes
Very easy

Given a hanging tree with (1 \leq n \leq 100) vertices, numbered from to n-1. You are required to answer (1 \leq m \leq 100) queries about the least common ancestor for given pairs of vertices.

The queries are generated as follows. Given a_1, a_2 and numbers , , , the numbers a_3, \ldots, a_{2m} are generated by the formula: a_i = (x \cdot a_{i-2} + y \cdot a_{i-1} + z) \bmod n. The first query is of the form \langle a_1, a_2 \rangle. If the answer to the (i-1)-th query is , then the -th query is \langle (a_{2i-1} + v) \bmod n, a_{2i} \rangle.

Input

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

The second line contains n-1 integers, where the -th integer gives the parent number of vertex .

The third line contains two integers from to n-1: a_1 and a_2.

The fourth line contains three non-negative integers , , , none exceeding 10^9.

Output

Output the sum of the vertex numbers of the answers to all queries.

Examples

Example 1

Loading
Loading