This page is still under construction.

Parts of this page are still being built. What you see may change.

Increasing Speed Limits

Time limit5sMemory limit512 MB

Summary
Given a sequence generated by a small recurrence, count strictly increasing subsequences by position, modulo 1000000007.
Level

Medium6 of 10

Topics
Dynamic programming, Sorting, Combinatorics, Prefix sum
Solved
No attempts yet

Problem

You were driving along a highway when the road police caught you for speeding. They had been following you for a while, and they were amazed that you kept accelerating the whole time without ever touching the brakes. Now you need an excuse that sounds plausible.

You decided to say "every speed limit sign I saw was in increasing order, so I kept accelerating". The officer laughs, tells you every sign placed along the stretch of highway you drove, in order, and says you could not have been lucky enough to see only the ones that happened to be in increasing order.

Estimate how much luck that would take. Count how many subsequences of the given sequence are strictly increasing. The empty subsequence does not count, since it would mean you looked at no sign at all.

Subsequences are distinguished by position, not by value. In the sequence (1,4,2,3,5,5)(1, 4, 2, 3, 5, 5), the strictly increasing subsequence with values (1,2,5)(1, 2, 5) is counted twice, because there are two ways to pick the 55.

Input

The first line contains the number of test cases NN. The NN test cases follow.

The first line of each test case contains nn, mm, XX, YY and ZZ, separated by single spaces. nn is the length of the speed limit sequence, and mm is the length of the generating array AA. The next mm lines contain the elements of AA, one integer per line, from A[0]A[0] to A[m−1]A[m-1].

Using AA, XX, YY and ZZ, the following pseudocode prints the speed limit sequence in order. mod is the remainder operation.

for i = 0 to n-1
  print A[i mod m]
  A[i mod m] = (X * A[i mod m] + Y * (i + 1)) mod Z

The input is generated this way only to keep the input small. It has nothing to do with how the problem is solved.

Limits:

  • 1≤N≤201 \le N \le 20
  • 1≤m≤1001 \le m \le 100
  • 0≤X≤1090 \le X \le 10^9
  • 0≤Y≤1090 \le Y \le 10^9
  • 1≤Z≤1091 \le Z \le 10^9
  • 0≤A[i]<Z0 \le A[i] < Z
  • 1≤m≤n≤10001 \le m \le n \le 1000

Output

For each test case, print one line in the form Case #T: S, where TT is the test case number and SS is the number of non-empty strictly increasing subsequences modulo 10000000071000000007.

Hint

With n=6n = 6, m=2m = 2, X=2X = 2, Y=1000000000Y = 1000000000, Z=6Z = 6 and A=(1,2)A = (1, 2), the pseudocode prints 1,2,0,0,0,41, 2, 0, 0, 0, 4.

Examples1

  1. Example 1

    Input
    2
    5 5 0 0 5
    1
    2
    1
    2
    3
    6 2 2 1000000000 6
    1
    2
    
    Expected output
    Case #1: 15
    Case #2: 13