QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100 Hackable ✓

#13620. 矩形

统计

In this problem, we define $0^0 = 1$.

Snuke is solving a problem using dynamic programming. Specifically, she designed the following recurrence relation: $$ F(i, j) = \begin{cases} 0, & \text{if $i = 0$;} \\ f_i, & \text{if $i > 0$ and $j = 0$;} \\ aF(i - 1, j) + bF(i, j - 1) + c, & \text{if $i > 0$ and $j > 0$.} \end{cases} $$ where $i, j$ are non-negative integers, $a, b, c$ are given constants, and $f_i$ is a given sequence.

Snuke needs to find $F(n, m)$, so she calculated $F(i, j)$ for $1 \le i \le n, 1 \le j \le m$. These values form an $n \times m$ matrix.

Takahashi wants you to tell her about this matrix. To avoid excessive output, you only need to output the hash of this matrix.

Specifically, given an integer $h$ and a prime $p$, you need to output the value of $$ \left( \sum_{i = 1}^{n} \sum_{j = 1}^{m} F(i, j) \, h^{(i - 1)m + (j - 1)} \right) \bmod p $$

Input

The input is read from standard input.

The first line contains an integer $T$, representing the subtask number.

The second line contains four integers $n, m, h, p$.

The third line contains three integers $a, b, c$.

The fourth line contains $n$ integers $f_1, \ldots, f_n$.

Output

Output to standard output.

Output a single line containing an integer, representing the hash of the matrix.

Examples

Input 1

1
2 2 2 998244353
2 1 1
0 1

Output 1

93

Note 1

The matrix mentioned in the problem description is $\left( \begin{matrix} 1 & 2 \\ 4 & 9 \end{matrix} \right)$, and its hash value is $$ 1 \times 2^0 + 2 \times 2^1 + 4 \times 2^2 + 9 \times 2^3 = 93. $$

Input 2

2
9 100000 5 998244353
5 4 7
6 5 6 9 3 7 4 5 2

Output 2

623270548

Input 3

3
9 1000000000 5 235497281
5 0 7
6 5 6 9 3 7 4 5 2

Output 3

211538270

Constraints

For all data, it is guaranteed that:

  • $1 \le n \le 10^6$
  • $1 \le m \le 10^9$
  • $10^8 \le p \le 10^9$, $p$ is a prime
  • $0 \le h, a, b, c, f_i < p$
Subtask Number$n \le$$m \le$Special PropertiesScoreDependent Subtasks
1$1000$$1000$$p = 998244353$5None
2$10^5$$10^5$241
3$10^6$$10^9$$b = 0$10None
4$c = 0$28
5$f_i = 0$30
6None31, 2, 3, 4, 5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.