Need for sum thing#4606

Need for sum thing

2 seconds
256 megabytes
Easy

You are given a triangular table consisting of rows. The first row has one element, the second row has three elements, the third row has five elements, and so on: the -th row contains exactly 2i-1 elements. The central elements of all rows are aligned in one column, forming a right isosceles triangle with the right angle at the top and the hypotenuse at the bottom row.

Each element of the table is a digit from to .

Your task is to answer queries about the table. Each query asks you to find the sum of all elements in a specified triangular region of the table. Each query is described by three numbers: r_i, c_i, k_i.

The region for the -th query is a right isosceles triangle with k_i rows and its apex at (r_i, c_i). The first row of this triangle consists of the single element (r_i, c_i). The second row consists of three elements: (r_i + 1, c_i), (r_i + 1, c_i + 1), and (r_i + 1, c_i + 2). The third row consists of five elements: (r_i + 2, c_i), (r_i + 2, c_i + 1), , (r_i + 2, c_i + 4), and so on.

Since the number of queries may be very large, they are generated programmatically as follows:

A_1 = 1
A_i = (1234567 \cdot A_{i-1} + 7654321) \bmod 1000000007, \quad \text{for } 2 \leq i \leq Q
r_i = A_i \bmod N + 1
c_i = A_i \bmod (2r_i - 1) + 1
k_i = A_i \bmod (N - r_i + 1) + 1

Input

The first line contains two integers and (1 \leq N \leq 10^3, 0 \leq Q \leq 5 \cdot 10^6): the number of rows in the triangle and the number of queries.

The next lines describe the table. The -th line contains exactly 2i-1 digits, which correspond to the elements of the -th row. There are no spaces between the digits in each row.

Output

Print the sum of answers for all the queries.

Examples

Example 1

Loading
Loading