QOJ.ac

QOJ

Time Limit: 5.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓

#8717. Dice

Statistics

There are $n$ dice, each with $m+1$ faces, labeled with the numbers $0 \sim m$. The probability that the $i$-th die lands on the face with number $j$ is always $p_{i,j}$.

You plan to roll dice $q$ times. In the $i$-th roll, you throw all dice with indices in the range $[l_i, r_i]$. Let $sum$ be the sum of the numbers on the faces of these dice. If $sum \le m$, you receive $b_{sum}$ happiness; otherwise, you receive nothing. What is the expected happiness you receive for each round? The answer should be modulo $10^9 + 7$.

Input

The first line contains three positive integers $n, m, q$ ($1 \le n \le 1500, 1 \le m \le 200, 1 \le q \le 6 \times 10^5$).

The second line contains $m+1$ non-negative integers, representing $b_0, \dots, b_m$ ($0 \le b_i < 10^9 + 7$).

The next $n$ lines each contain $m+1$ non-negative integers $p'_{i,0}, \dots, p'_{i,m}$ ($p'_{i,j} \ge 0, \sum_{j=0}^m p'_{i,j} = 10^9 + 8$), representing $p_{i,j} = \frac{p'_{i,j}}{10^9 + 8}$.

The next $q$ lines each contain two positive integers $l_i, r_i$ ($1 \le l_i \le r_i \le n$).

Output

Output $q$ lines, each containing a non-negative integer representing the answer modulo $10^9 + 7$.

Examples

Input 1

3 3 3
4 3 2 1
0 1 0 1000000007
0 500000004 0 500000004
0 0 500000004 500000004
1 1
1 2
1 3

Output 1

3
1
0

Input 2

3 3 6
4 3 2 1
1000000007 0 1 0
1000000007 1 0 0
1000000007 0 1 0
1 1
1 2
1 3
2 2
2 3
3 3

Output 2

2
1
0
3
1
2

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.