LR Shirts

 There are 

people standing in a line from left to right. The -th person from the left has a height . is a permutation of to

.

First, you must assign each of them a shirt with the letter

or

on it. Then you may perform the following operation any number of times (possibly zero):

  • Choose a person with
  • on their shirt and move them to the left end of the line.
  • Choose a person with
    • on their shirt and move them to the right end of the line.

    For example, if

    and we assign the shirts to the people as , then if we move person to the right end of the line the arrangement of people becomes

    An assignment of shirts is called good if it is possible to sort the people in the line in non-decreasing order of height from left to right by using this operation zero or more times.

    How many good assignments of shirts exist? Since this number may be large, print it modulo

    .

    One assignment of shirts is different from another assignment if there exists at least one person who is assigned a shirt with a different letter on it.

    Input Format

    • The first line of the input contains a single integer
    - the number of test cases. The description of
  • test cases follows.
  • The first line of each test case contains a single integer
  • - the number of people.
  • The second line contains
  • space-separated integers - the heights of the
    • people.

    Output Format

    For each test case, print a single line containing one integer ― the number of good assignments of shirts modulo

    .

    Constraints

  • is a permutation of
  • .
  • the sum of
  • over all test cases does not exceed

    Sample Input 1

    6
    2
    2 1
    1
    1
    5
    1 2 3 4 5
    4
    1 3 4 2
    7
    6 1 7 3 5 4 2
    9
    6 5 9 3 7 1 2 4 8
    

    Sample Output 1

    3
    2
    32
    7
    11
    16
    

    Explanation

    Test case 1:

    • For the assignment
    , we can sort the line in non-decreasing order by moving person
  • to the left end of the line.
  • For the assignment
  • , we can sort the line in non-decreasing order by moving person
  • to the right end of the line.
  • For the assignment
  • , we can sort the line in non-decreasing order by moving person to the right end of the line or by moving person
  • to the left end of the line.
  • For the assignment
    • , all possible operations don't change the order of people in the line. So it is impossible to sort line in non-descending order of height.

    Test case 2: A line with a single person is always sorted in non-decreasing order regardless of the operations performed, so both possible assignments of shirts are valid.

    Test case 3: For any assignment of shirts, we can sort the line in non-descending order by performing zero operations as the line is already sorted in non-decreasing order. So all

    assignments of shirts are valid.

    Test case 4: The

    good assignments of shirts are:

  • Previous
    Next Post »