Boy Yaroslav wanted to eat $$$n$$$ whoppers for lunch. First he eats the first whopper, then the second, the third, and so on. The tastiness of the $$$i$$$-th whopper is $$$a_i$$$.
Since Yaroslav has ADHD, it is hard for him to eat a whopper when its tastiness is different from the tastiness of the previous whopper he ate, so he can do this exactly $$$m$$$ times. He became interested in how many different sequences of whoppers he can eat if the tastiness of each whopper can be any integer from $$$1$$$ to $$$k$$$.
More formally, you need to count the number of sequences $$$a$$$ of length $$$n$$$, consisting of numbers from $$$1$$$ to $$$k$$$, such that there are exactly $$$m$$$ indices $$$i$$$ ($$$2 \le i \le n$$$) for which $$$a_i \ne a_{i-1}$$$.
Since the number of such sequences can be very large, print the answer modulo $$$10^9 + 7$$$.
The only line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$0 \le m \lt n \le 10^7$$$, $$$1 \le k \le 10^9$$$) — the number of whoppers, the number of transitions between whoppers with different tastiness, and the maximum possible tastiness of a whopper.
Print one integer — the number of whopper sequences that Yaroslav can eat, modulo $$$10^9 + 7$$$.
The tests for this problem consist of four groups. Points for each group are given only if all tests of this group and all tests of some previous groups are passed. Please note that passing the sample tests is not required for some groups. The final score for each group is equal to the maximum score obtained for this group over all submissions.
| Subtask | Points | Additional constraints | Required subtasks | |
| $$$n$$$ | additionally | |||
| $$$1$$$ | $$$20$$$ | $$$n \le 8$$$ | $$$k \le 8$$$ | S |
| $$$2$$$ | $$$15$$$ | — | $$$m = n - 1$$$ | — |
| $$$3$$$ | $$$25$$$ | $$$n \le 1\,000$$$ | — | S, 1 |
| $$$4$$$ | $$$40$$$ | — | — | S, 1–3 |
3 2 4
36
4 2 4
108
8 4 5
44800
Working in the kitchen of "Burger King" is real art, especially when it comes to making the legendary Whopper!
In front of you there is a rectangular organizer with ingredients, represented as a matrix of size $$$n \times m$$$. The organizer is filled with so many different components (from a crunchy pickled cucumber to the signature sauce) that each of them is unique. Each cell has an importance level from $$$0$$$ to $$$n \cdot m - 1$$$, and every level appears exactly once. The importance level of the ingredient located in row $$$i$$$ and column $$$j$$$ is $$$a_{i, j}$$$.
To make a burger, you must use one careful move with a special rectangular spatula and take some rectangular submatrix from the organizer. However, the spatula has limited size, so the area of the chosen submatrix, that is, the number of cells in it, must not exceed $$$k$$$.
The taste of the perfect Whopper is determined by how consistently the basic ingredients are collected. Formally, the taste of the burger is equal to the $$$\operatorname{MEX}$$$ of the set of importance levels of the ingredients in the chosen submatrix.
Recall that $$$\operatorname{MEX}$$$ (Minimum Excluded) of a set of numbers is the smallest non-negative integer that does not belong to this set. For example, $$$\operatorname{MEX}$$$ of the set $$$\{0, 1, 3, 4\}$$$ is $$$2$$$, and $$$\operatorname{MEX}$$$ of the set $$$\{1, 2\}$$$ is $$$0$$$.
While the restaurant manager turned away to the cash desk, you have a chance to use your sleight of hand and swap the contents of any two different cells of the organizer at most once. You may also choose not to change anything at all.
Determine the maximum possible taste of the Whopper that you can get if you optimally make at most one swap and then choose a rectangular submatrix of ingredients.
In the first line, three integers $$$n$$$, $$$m$$$ and $$$k$$$ are given ($$$1 \le n, m \le 200\,000$$$, $$$1 \le n \cdot m \le 200\,000$$$, $$$1 \le k \le n \cdot m$$$) — the dimensions of the organizer matrix and the maximum allowed area of the submatrix.
In the next $$$n$$$ lines, $$$m$$$ integers $$$a_{i, j}$$$ are given in each line ($$$0 \le a_{i, j} \lt n \cdot m$$$) — the matrix of ingredients. It is guaranteed that each number from $$$0$$$ to $$$n \cdot m - 1$$$ appears in the matrix exactly once.
Print one integer — the maximum possible $$$\operatorname{MEX}$$$ of a submatrix with area at most $$$k$$$ that can be obtained after making at most one swap of elements.
The tests for this problem consist of five groups. Points for each group are given only if all tests of this group and all tests of some previous groups are passed. Please note that passing the sample tests is not required for some groups. The final score for each group is equal to the maximum score obtained for this group over all submissions.
| Subtask | Points | Additional constraints | Required subtasks |
| $$$1$$$ | $$$12$$$ | $$$n, m \le 20$$$ | S |
| $$$2$$$ | $$$15$$$ | $$$n = 1$$$ or $$$m = 1$$$ | — |
| $$$3$$$ | $$$18$$$ | $$$k \le 100$$$ | S, 1 |
| $$$4$$$ | $$$22$$$ | the answer does not require a swap | — |
| $$$5$$$ | $$$33$$$ | — | S, 1–4 |
3 3 40 1 82 7 63 5 4
4
1 5 30 1 4 3 2
3
What can be better than a whopper? Only a double whopper! And what about a $$$k$$$-whopper? Bob wants to eat like a pig, but Alice wants to stop him!
A reform was made in "Burger King", and now there are $$$n$$$ cash desks there, numbered from $$$0$$$ to $$$n - 1$$$. For each cash desk, two numbers are given: $$$a_i$$$ and $$$k_i$$$ — the change in the number of patties in the whopper after visiting this cash desk, and its importance, respectively. The value $$$a_i$$$ can be negative, then the number of patties decreases.
All visitors start at cash desk $$$n - 1$$$ and then move between cash desks until they reach cash desk $$$0$$$. While at cash desk with number $$$i$$$ ($$$0 \lt i \lt n$$$), one can move either to cash desk $$$i - 1$$$, or choose a number $$$b$$$ ($$$1 \lt b \le k_i$$$) and move to cash desk $$$\lfloor i / b \rfloor$$$.
Before visiting cash desk $$$n-1$$$, Bob's whopper contains $$$10^{100}$$$ patties (did you think we were bluffing in the first paragraph?). When visiting cash desk with number $$$i$$$, exactly $$$a_i$$$ patties are added to the whopper, and it is impossible not to take them. Alice and Bob make moves alternately, and Alice moves first. Alice wants to minimize the final number of patties, while Bob wants to maximize it. On their move, Alice or Bob chooses the next cash desk to move to.
Determine how many patties will be in the whopper if both players play optimally.
The first line contains one integer $$$n$$$ ($$$2 \le n \le 200\,000$$$) — the number of cash desks in "Burger King".
The second line contains $$$n$$$ integers $$$a_i$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the changes in the number of patties at the cash desks.
The third line contains $$$n$$$ integers $$$k_i$$$ ($$$2 \le k_i \le n$$$) — the importance values of the cash desks.
Let the whopper contain $$$10^{100}+x$$$ patties after optimal play by Alice and Bob. Then you need to print the single integer $$$x$$$.
The tests for this problem consist of seven groups. Points for each group are given only if all tests of this group and all tests of some previous groups are passed. Please note that passing the sample tests is not required for some groups. The final score for each group is equal to the maximum score obtained for this group over all submissions.
| Subtask | Points | $$$n$$$ | Additionally | Required subtasks |
| 1 | 10 | $$$n \le 10$$$ | — | S |
| 2 | 15 | $$$n \le 20$$$ | $$$k_i = 2$$$ | — |
| 3 | 15 | — | $$$a_i \ge 0$$$ | — |
| 4 | 15 | $$$n \le 1\,000$$$ | — | S, 1, 2 |
| 5 | 20 | $$$n \le 60\,000$$$ | — | S, 1, 2, 4 |
| 6 | 10 | $$$n \le 100\,000$$$ | — | S, 1, 2, 4, 5 |
| 7 | 15 | — | — | S, 1–6 |
51 -1 2 3 -12 2 3 4 3
-1
50 -5 2 1 -55 2 2 2 2
-8
In the times of the Great Angus, when ancient recipes in the kingdom of "Burger King" were only starting to appear, each element of the array was treated as a separate ingredient for the sacred burger. The number $$$a_i$$$ meant which ingredient was on the $$$i$$$-th shelf of the royal storage, and there were $$$n$$$ shelves in total.
But on the day of the Great Angus Celebration, the priests were given an unusual task. From the segment of the storage from the $$$l$$$-th to the $$$r$$$-th shelf, they had to collect exactly $$$k$$$ baskets of ingredients, such that:
Here, $$$\operatorname{MEX}$$$ — is the smallest non-negative integer that is not in the chosen basket: it was considered the number of the missing ingredient in the great Angus recipe.
The priests of "Burger King" believed that if they count the number of ways to collect the baskets like this on a segment of the storage, then on that day the Angus will be especially juicy, and the kingdom will be full and happy.
That is exactly what the wise men do: for each query with segment borders $$$[l, r]$$$ and number $$$x$$$, they count how many ways there are to choose a set of $$$k$$$ pairwise non-intersecting subsequences $$$a_l, a_{l+1}, \ldots, a_r$$$ such that each subsequence has $$$\operatorname{MEX}$$$ equal to $$$x$$$.
Note that two subsequences are considered different if they consist of elements with different indices in the array $$$a$$$. For example, in the array $$$a = [0, 0, 0]$$$ on the segment $$$[1,3]$$$ there are $$$7$$$ non-empty subsequences with $$$\operatorname{MEX}$$$ equal to $$$1$$$.
You have to act as a wise man and count the number of such ways. Since this number can be very large, it is enough to print the answers modulo $$$10^9 + 7$$$.
In the first line, three integers $$$n$$$, $$$q$$$ and $$$k$$$ are given ($$$1 \le n, q \le 100\,000$$$, $$$1 \le k \le \min{(n, 1000)}$$$) — the number of shelves, the number of queries, and the number of baskets, respectively.
In the second line, $$$n$$$ integers $$$a_i$$$ are given ($$$0 \le a_i \le n$$$) — the ingredients on the shelves.
Each of the next $$$q$$$ lines contains one query. It contains three integers $$$l_i$$$, $$$r_i$$$ and $$$x_i$$$ ($$$1 \le l_i \le r_i \le n$$$, $$$1 \le x_i \le n$$$) — the segment borders and the required value of $$$\operatorname{MEX}$$$, respectively.
Print $$$q$$$ lines. Each of them must contain one integer — the answer to the corresponding query modulo $$$10^9 + 7$$$.
The tests for this problem consist of eight groups. Points for each group are given only if all tests of this group and all tests of some previous groups are passed. Please note that passing the sample tests is not required for some groups. The final score for each group is equal to the maximum score obtained for this group over all submissions.
| Subtask | Points | $$$n$$$ | Additionally | Required subtasks |
| 1 | 7 | $$$n \le 8$$$ | — | S |
| 2 | 4 | — | $$$k = n$$$ | — |
| 3 | 12 | — | $$$a_i = 0$$$ | — |
| 4 | 13 | $$$n \le 3\,000$$$ | $$$k = 1$$$ | — |
| 5 | 14 | $$$n \le 3\,000$$$ | — | S, 1, 4 |
| 6 | 16 | — | $$$k = 1$$$ | 4 |
| 7 | 13 | — | $$$l_i = 1$$$, $$$r_i = n$$$ | — |
| 8 | 21 | — | — | S, 1–7 |
3 2 20 1 01 3 12 3 2
1 0
6 3 10 1 2 3 4 53 6 11 4 21 6 6
0 2 1
8 8 20 1 2 0 0 1 2 11 6 14 5 14 4 12 5 21 8 31 8 21 8 14 7 2
18 1 0 0 144 72 54 0