问题 J: Calculate

内存限制:256 MB 时间限制:12 S
题面:传统 评测方式:文本比较 上传者:
提交:3 通过:1

题目描述

Ming is on a map. There are n vertexes in the map. For each vertex, there is only one one-way edge that can reach another vertex on the map.

Ming has a number y in his hand, and when he reaches a certain vertex i, the number in his hand will change to �×��+��y×ki+bi.

There are several inquiries, each query Ming will start from a vertex x and walk l steps. At each step Ming starts from the current vertex and proceeds along the outgoing arc of that vertex to the next vertex. The initial number in his hand is y, and he wants to know what the number in his hand will become in the end.The answer is modulo 109+7109+7

输入格式

The first line, input a positive integer �(1≤�≤5)T(1T5), representing the number of test data.

For each test, first line input a row of two positive integers �,�(1<�,�≤105)n,q(1<n,q105), representing the number of points in the map and the number of queries.

The next line inputs n positive integers ��(1≤��≤105)ki(1ki105) representing the value of k on each vertex

The next line inputs n positive integers ��(1≤��≤105)bi(1bi105) representing the value of b on each vertex

The next line inputs n positive integers ��(1≤��≤�)pi(1pin) represents the vertex reachable from vertex i, guaranteeing ��≠�pi=i.

The next q lines,each line has three positive integers ��,��,��(1≤��≤�,1≤��≤109,1≤��≤105)xi,li,yi(1xin,1li109,1yi105) for a query,��xi is the vertex where Ming started, ��li is the number of steps Ming took, and ��yi is the number in Ming's hand.

输出格式

For each query, output a line with an integer representing the answer.The answer could be so large that you have to take mod 109+7109+7.

输入样例 复制

1
5 5
2 3 1 2 4
3 4 2 1 2
2 3 5 1 4
1 2 4
2 4 5
3 3 4
4 5 2
5 2 1

输出样例 复制

18
125
77
221 
9