Flight

아직 제출이 없습니다시간 제한1초메모리 제한256 MB

문제

You are given a tree with NN vertices. The distance between two vertices is the number of edges lying on the simple path between them.

There are QQ queries. Each query is specified by two vertices uu and vv and an integer dd. A pair of vertices is called good if the distance between them is not less than dd. In each step, you can only move between a good pair of vertices. Now your task is to calculate the minimum number of steps you have to make in order to get from uu to vv. If you can not reach the destination, the answer is 1-1.

A lot of queries are given to you to make this problem difficult.

입력

The first line of input contains three integers NN, QQ and  MM: the number of vertices, the number of queries and the upper bound for dd. (1N21051 \leq N \leq 2 \cdot 10^5, 1Q1061 \leq Q \leq 10^6, 1M21051 \leq M \leq 2 \cdot 10^5).

The second line contains N1N - 1 integers f_2f\_2, f_3f\_3, \ldots, f_Nf\_N which mean that, for every ii such that 2iN2 \leq i \leq N, there is an edge between vertices ii and f_if\_i in the tree (1f_i<i1 \leq f\_i < i).

The third line contains six integers u_1u\_1, v_1v\_1, d_1d\_1, AA, BB and CC (1u_1,v_1N1 \leq u\_1, v\_1 \leq N, 0d_1<M0 \leq d\_1 < M, 104A,B,C210410^4 \leq A, B, C \leq 2 \cdot 10^4).

The first query is specified by u_1u\_1, v_1v\_1 and d_1d\_1.

The ii-th (2iQ2 \leq i \leq Q) query is specified by u_iu\_i, v_iv\_i and d_id\_i which are generated by the following rules:

  • u_i=((Au_i1+B+ans_i1)modN)+1u\_i = ((A \cdot u\_{i - 1} + B + \mathit{ans}\_{i - 1}) \bmod N) + 1,
  • v_i=((Bv_i1+C+ans_i1)modN)+1v\_i = ((B \cdot v\_{i - 1} + C + \mathit{ans}\_{i - 1}) \bmod N) + 1,
  • d_i=(Cd_i1+A+ans_i1)modMd\_i = (C \cdot d\_{i - 1} + A + \mathit{ans}\_{i - 1}) \bmod M.

Here, ans_k\mathit{ans}\_{k} is the answer for query kk.

출력

Output the integer S=_i=1Qi(ans_i+1).S = \sum\limits\_{i = 1}^{Q} i \cdot (\mathit{ans}\_i + 1)\text{.}