# ADA University - February 12 - LCA

# LCA - 2

The hanging tree with **n** (**1** ≤ **n** ≤ `10`

) vertices numbered from ^{5}**0** to **n** - **1** is given. Answer **m** (**1** ≤ **m** ≤ `10`

) queries about Least Common Ancestor for pair of vertices.^{7}

The queries are numbered in the next way. Given values `a`

, _{1}`a`

and _{2}**x**, **y** и **z**. Numbers `a`

, ..., _{3}`a`

are generated in the next way:_{2m}

`a`

= (_{i}**x** * `a`

+ _{i-2}**y** * `a`

+ _{i-1}**z**) mod **n**.

First query has the form <`a`

, _{1}`a`

>. If the answer to the (_{2}**i** - **1**)-th query is **v**, then the **i**-th query has the form <(`a`

+ _{2i-1}**v**) mod **n**, `a`

>._{2i}

#### Input

First line contains two numbers **n** and **m**. The root of the tree has number **0**. Second line contains **n** - **1** integers, **i**-th of these numbers equals to the parent of the vertex **i**. Third line contains two integers in the range from **0** to **n** - **1**: `a`

and _{1}`a`

. Fourth line contains three integers: _{2}**x**, **y** and **z**, they are non-negative and no more than `10`

.^{9}

#### Output

Print the sum of the vertex numbers - the answers to all queries.

3 2 0 1 2 1 1 1 0

2

1 2 0 0 1 1 1

0