Binary Search Tree

No attempts yetTime limit2sMemory limit256 MB

Problem

A binary search tree is a binary tree, and it may be empty. A non-empty binary search tree satisfies the following properties.

  1. Every node holds a distinct key.
  2. Every key in the left subtree of a node is smaller than that node's key.
  3. Every key in the right subtree of a node is larger than that node's key.
  4. The left subtree and the right subtree are themselves binary search trees.

Searching for a key kk in a binary search tree TT works as follows. Start at the root; if TT is empty, the search fails. Otherwise, compare kk with the root's key. If they are equal, the search succeeds. If kk is smaller than the root's key, continue in the left subtree; if it is larger, continue in the right subtree.

To insert a new key kk that is not already in TT, first search for kk in TT. Since kk is not present, the search fails, and kk is inserted as a new node at the empty spot where the search stopped.

In this problem we work with binary search trees whose keys are 1,2,,N1, 2, \dots, N. Given a permutation a1,a2,,aNa_1, a_2, \dots, a_N of {1,2,,N}\{1, 2, \dots, N\}, inserting a1a_1 through aNa_N in order into an empty tree produces one binary search tree.

Different permutations may produce the same tree. For example, inserting the permutation 2 1 4 3 52\ 1\ 4\ 3\ 5 in order builds the tree whose root is 22 with left child 11 and right child 44, where 44 has left child 33 and right child 55. The permutation 2 4 3 1 52\ 4\ 3\ 1\ 5 builds exactly the same tree. Among all permutations of {1,2,3,4,5}\{1, 2, 3, 4, 5\}, there are 88 that build this tree.

Given NN and a permutation PP, write a program that counts how many permutations build the same binary search tree as PP.

Input

The first line contains the number of test cases TT (T40,320T \le 40{,}320).

Each test case consists of two lines. The first line contains the number of keys NN (1N201 \le N \le 20), and the second line contains a permutation of {1,2,,N}\{1, 2, \dots, N\} of length NN, separated by spaces.

Output

For each test case, print on its own line the number of permutations that build the same binary search tree as the given permutation, modulo 9,999,9919{,}999{,}991.