Sergio is analyzing the merge algorithm for his algorithms assignment. His professor has written the following algorithm:
std::vector<int> merge(std::vector<int> A, std::vector<int> B) {
std::vector<int> C;
int i=0, j=0;
while (i < A.size() && j < B.size()) {
if (A[i] <= B[j]) {
C.push_back(A[i]);
i += 1;
} else {
C.push_back(B[j]);
j += 1;
}
}
while (i < A.size()) {
C.push_back(A[i]);
i += 1;
}
while (j < B.size()) {
C.push_back(B[j]);
j += 1;
}
return C;
}
To do this, he has $$$M$$$ non-empty sequences $$$S_1$$$, $$$S_2$$$, ..., $$$S_M$$$, and performs the following operation $$$S_1 =$$$ merge($$$S_1, S_i$$$) for $$$i$$$ from $$$2$$$ to $$$M$$$. Thus, finally, to carry out his analysis, he wants to count the number of distinct valid initial sequences that produce the same final sequence $$$S_1$$$.
Two initial sequences (sequences of sequences) $$$S_1, S_2, \ldots, S_M$$$ and $$$S'_1, S'_2, \ldots, S'_m$$$ are distinct if there exists an index $$$i$$$ ($$$1 \le i \le M$$$) such that $$$S_i \neq S'_i$$$. Similarly, two sequences $$$A$$$ and $$$B$$$ are distinct if they differ in their number of elements or there exists an index $$$j$$$ ($$$1 \le j \le |A_i|$$$) such that $$$A_j \neq B_j$$$.
Note that the final $$$S_1$$$ sequence is not necessarily sorted.
The first line of input contains two integers $$$M$$$ and $$$N$$$ ($$$1 \le N, M \le 3000$$$), indicating the number of initial sequences and the size of the final sequence $$$S_1$$$, respectively.
The second line contains $$$N$$$ positive integers, $$$S_{1 i}$$$ ($$$1 \le S_{1 i} \le N$$$), separated by spaces, representing the final sequence $$$S_1$$$,
Print a line with a single integer modulo $$${10}^9 + 7$$$, indicating the number of possible initial sequences.
3 61 2 3 4 5 6
540
3 81 3 4 2 5 2 7 8
540
4 82 4 1 4 1 2 3 2
0
| Name |
|---|


