Under construction

Still being built. Things may change

Let Me Tell You a Story (Small)

Time limit5sMemory limit512 MB

Summary
Count the firing orders that delete ministers until the remaining pay sequence never rises down the ranks, modulo 10007.
Level

Medium7 of 10

Topics
Dynamic programming, Combinatorics
Solved
No attempts yet

Problem

An old storyteller keeps changing his mind about how one story ended.

King Tyrone had four ministers. From the highest rank down, their weekly pay was 7, 4, 6 and 6 gold pieces. When the pay list reached the newspaper, the second minister protested that a minister of lower rank was paid more than he was. The king would not touch a salary and would not touch a rank, so he fired someone instead. In one version he fired the third minister, then the fourth. In another version he fired the first minister, who had nothing to do with the protest, and then fired the second minister because the protest was still standing. Both versions end the same way. The pay of the ministers who are left never rises as you read down the ranks, nobody protests, and the firings stop.

The storyteller no longer remembers the order of the firings, only the rules. There were NN ministers, numbered 11 to NN from the highest rank to the lowest, and minister ii was paid aia_i gold pieces per week. A protest happens whenever two serving ministers i<ji < j satisfy ai<aja_i < a_j. While at least one protest stands, the king fires exactly one of the ministers still serving. He may fire any of them, including a minister who takes no part in any protest. The ministers who remain keep their relative ranks. Once the pay of the serving ministers never rises as you read down the ranks, nobody protests and nobody else is fired.

A story is the sequence of fired ministers written in the order they were fired. Two stories are different when those sequences are different. If nobody protests at the start, nobody is fired and the empty sequence is the only story. Count the stories the storyteller could tell, modulo 10007.

Input

The first line contains the number of test cases TT. Each test case takes two lines. The first line contains the number of ministers NN. The second line contains NN space separated integers a1a_1 to aNa_N, the weekly pay of the first minister through the NNth minister.

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤1001 \le N \le 100
  • 1≤ai≤100001 \le a_i \le 10000

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the number of possible stories modulo 10007.

Examples4

  1. Example 1

    Input
    3
    4
    7 4 6 6
    8
    90 80 70 60 50 50 40 30
    2
    7 8
    
    Expected output
    Case #1: 14
    Case #2: 1
    Case #3: 2
    
  2. Example 2

    Input
    2
    1
    1
    1
    10000
    
    Expected output
    Case #1: 1
    Case #2: 1
    
  3. Example 3

    Input
    3
    2
    1 10000
    2
    10000 1
    2
    5 5
    
    Expected output
    Case #1: 2
    Case #2: 1
    Case #3: 1
    
  4. Example 4

    Input
    4
    3
    1 2 3
    4
    1 2 3 4
    3
    3 1 2
    3
    2 1 3
    
    Expected output
    Case #1: 6
    Case #2: 24
    Case #3: 4
    Case #4: 5