Decreasing Max Partitioning

3.5

2 votes
Medium, Dynamic programming
Problem

You have been given an array A of N integers A1,A2...AN.
The Partition P of the array is defined by two integers startP and endP and it contains all the elements lying at positions [startP,startP+1...endP]. That is, partition P refers to subarray [AstartP,AstartP+1...AendP] such that startPendP.
The value of partition is defined as maximum element present in the partition i.e. value(P)=maximum(AstartP,AstartP+1...AendP).

You have to find out total number of ways to divide array A into sequence of partitions such that each element of A belongs to exactly one partition and value of each partition is not less than the value of next partition.

Formally, Suppose you divided array A into k partitions P1,P2...Pk for some k1, then following conditions should be satisfied:
1. startP1=1 and endPk=N.
2. startPiendPi for all ik.
3. startPi+1=endPi+1 for all i<k.
4. value(Pi)value(Pi+1) for all i<k.

You have to find out total number of ways to divide array A into such sequence of partitions.

INPUT:
First line of input consists of integer T denoting total number of testcases.
First line of each test case consists of an integer N. Next line consists of N integers A1,A2...AN.

Output:
Output total number of ways to divide the given array into partitions. Since output can be large, print the total number of ways modulus 109+7.

Constraints:
1T20
1N105
1Ai105

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In first sample case, all the 4 possible partitions are [[3,2,1]],[[3],[2,1]],[[3,2],[1]],[[3],[2],[1]].
In second sample case, only 2 partitions are possible [[1,3,2]] and [[1,3],[2]].

Editor Image

?