LCA Problem Revisited
Given a rooted tree with n (1 ≤ n ≤ 100000) vertices, labeled from 0 to n-1, you need to respond to m (1 ≤ m ≤ 10000000) queries regarding the lowest common ancestor (LCA) of pairs of vertices.
The queries are generated as follows: You are provided with numbers a_1, a_2, and constants x, y, and z. The sequence a_3, ..., a_2m is generated using the formula: ai = (x·a_{i-2} + y·a_{i-1} + z) mod n. The first query is < a_1, a_2 >. If the answer to the i-1-th query is v, then the i-th query is < (a_{2i-1} + v) mod n, a_2i >.
Input
The first line contains two integers: n and m. The root of the tree is vertex 0. The second line lists n-1 integers, where the i-th integer indicates the parent of vertex i. The third line provides two integers, a_1 and a_2, each in the range from 0 to n-1. The fourth line contains three non-negative integers: x, y, and z, each not exceeding 10^9.
Output
Output the sum of the vertex numbers that are the answers to all the queries.