#P16646. [GKS 2018 #C] Kickstart Alarm

[GKS 2018 #C] Kickstart Alarm

Problem Description

Shil has a very hard time waking up in the morning each day, so he decides to buy a powerful alarm clock to Kickstart his day. This Alarm is called a Kickstart Alarm. It comes pre-configured with KK powerful wakeup calls. Before going to bed, the user programs the clock with a Parameter Array consisting of the values A1A_1, A2A_2, ..., ANA_N. In the morning, the clock will ring KK times, with the ii-th wakeup call having power POWERi\text{POWER}_i.

To calculate POWERi\text{POWER}_i, the alarm generates all the contiguous subarrays of the Parameter Array and calculates the summation of the ii-th exponential-power of all contiguous subarrays. The ii-th exponential-power of subarray AjA_j, Aj+1A_{j+1}, ..., AkA_k is defined as $A_j \times 1^i + A_{j+1} \times 2^i + A_{j+2} \times 3^i + ... + A_k \times (k-j+1)^i$. So POWERi\text{POWER}_i is just the summation of the ii-th exponential-power of all the contiguous subarrays of the Parameter Array.

For example, if i=2i = 2, and A=[1,4,2]A = [1, 4, 2], then the ii-th exponential-power of AA would be calculated as follows:

  • 2-nd exponential-power of [1]=1×12=1[1] = 1 \times 1^2 = 1
  • 2-nd exponential-power of [4]=4×12=4[4] = 4 \times 1^2 = 4
  • 2-nd exponential-power of [2]=2×12=2[2] = 2 \times 1^2 = 2
  • 2-nd exponential-power of [1,4]=1×12+4×22=17[1, 4] = 1 \times 1^2 + 4 \times 2^2 = 17
  • 2-nd exponential-power of [4,2]=4×12+2×22=12[4, 2] = 4 \times 1^2 + 2 \times 2^2 = 12
  • 2-nd exponential-power of $[1, 4, 2] = 1 \times 1^2 + 4 \times 2^2 + 2 \times 3^2 = 35$

so the total is 7171.

Tonight, Shil is using his Kickstart Alarm for the first time. Therefore, he is quite worried about the sound the alarm might make in the morning. It may wake up the neighbors, or, worse yet, it may wake up the whole planet! However, calculating the power of each wakeup call is quite difficult for him. Given KK and the Parameter Array A1A_1, A2A_2, ..., ANA_N, can you help him by calculating the summation of power of each wakeup call: $\text{POWER}_1 + \text{POWER}_2 + ... + \text{POWER}_K$?

Input Format

The first line of the input gives the number of test cases, T. TT test cases follow. Each test case consists of one line with nine integers NN, KK, x1x_1, y1y_1, CC, DD, E1E_1, E2E_2 and FF. NN is the length of array AA, KK is the number of wakeup calls. Rest of the values are parameters that you should use to generate the elements of the array AA, as follows.

Use the recurrences below to generate xix_i and yiy_i for i=2i = 2 to NN:

  • $x_i = ( C \times x_{i-1} + D \times y_{i-1} + E_1 )$ modulo FF.
  • $y_i = ( D \times x_{i-1} + C \times y_{i-1} + E_2 )$ modulo FF.

We define Ai=(xi+yi)A_i = ( x_i + y_i ) modulo FF, for all i=1i = 1 to NN.

Output Format

For each test case, output one line containing Case #x: POWER, where x is the test case number (starting from 1) and POWER is the summation of POWERi\text{POWER}_i, for i=1i = 1 to KK. Since POWER could be huge, print it modulo 10000000071000000007 (109+710^9 + 7).

2
2 3 1 2 1 2 1 1 9
10 10 10001 10002 10003 10004
10005 10006 89273
Case #1: 52
Case #2: 739786670

Hint

In Sample Case #1, the Parameter Array is [3, 2]. All the contiguous subarrays are [3], [2], [3, 2].

For i = 1:

  • 1-st Exponential-power of [3] = 3×11=33 \times 1^1 = 3
  • 1-st Exponential-power of [2] = 2×11=22 \times 1^1 = 2
  • 1-st Exponential-power of [3, 2] = 3+2×21=73 + 2 \times 2^1 = 7

So POWER1\text{POWER}_1 is 12.

For i = 2:

  • 2-nd Exponential-power of [3] = 3×12=33 \times 1^2 = 3
  • 2-nd Exponential-power of [2] = 2×12=22 \times 1^2 = 2
  • 2-nd Exponential-power of [3, 2] = 3+2×22=113 + 2 \times 2^2 = 11

So POWER2\text{POWER}_2 is 16.

For i = 3:

  • 3-rd Exponential-power of [3] = 3×13=33 \times 1^3 = 3
  • 3-rd Exponential-power of [2] = 2×13=22 \times 1^3 = 2
  • 3-rd Exponential-power of [3, 2] = 3+2×23=193 + 2 \times 2^3 = 19

So POWER3\text{POWER}_3 is 24.

Limits

1T1001 \le T \le 100.

1x11051 \le x_1 \le 10^5.

1y11051 \le y_1 \le 10^5.

1C1051 \le C \le 10^5.

1D1051 \le D \le 10^5.

1E11051 \le E_1 \le 10^5.

1E21051 \le E_2 \le 10^5.

1F1051 \le F \le 10^5.

Small dataset (Test set 1 - Visible)

1N1001 \le N \le 100.

1K201 \le K \le 20.

Large dataset (Test set 2 - Hidden)

1N1061 \le N \le 10^6.

1K1041 \le K \le 10^4.