This page is still under construction.

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

Cartesian Trees

Time limit3sMemory limit256 MB

Summary
Count the number of distinct Cartesian trees (BST over keys 1..n, max-heap over given priorities) modulo 1e9+7, summed over test cases with total n up to 2e5.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Combinatorics, Stack
Solved
No attempts yet

Problem

Recently, at a university lecture, Vasya learned what a Cartesian tree is. A Cartesian tree is a binary tree where each node stores two values: a key and a priority. It is a search tree over the set of keys and a max-heap over the priorities, that is:

  • the key of any node in the left subtree of node vv is less than the key of node vv;
  • the key of any node in the right subtree of node vv is greater than the key of node vv;
  • the priorities of the children of node vv are not greater than the priority of node vv itself.

On a test, Vasya was given the following problem: given nn pairs of the form (key, value), the ii-th of which is (i,yi)(i, y_i), find how many ways there are to build a Cartesian tree using the number ii as the key of node ii and yiy_i as its priority. Since this number can be quite large, find its remainder modulo 109+710^9+7.

Two Cartesian trees are considered different if they have different roots, or if there is a node that has different ancestors in these trees.

Input

The first line contains a single positive integer tt: the number of test cases in the input. The descriptions of the test cases follow.

The description of each test case consists of two lines. The first line contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5): the number of nodes in the tree. The second line contains nn integers yiy_i (1≤yi≤1091 \le y_i \le 10^9): the priority of the ii-th node of the tree.

The sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

Output

For each test case, output on a separate line a single integer: the number of distinct Cartesian trees that can be built on the given set of priorities, modulo 109+710^9+7.

Examples1

  1. Example 1

    Input
    2
    4
    2 4 1 3
    6
    7 3 3 1 1 3
    
    Expected output
    1
    10