Suffix Array 3
Time limit5sMemory limit512 MB
Given a permutation built by moving and reversing intervals, count the strings whose suffix array equals it, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Array, Combinatorics, Math, Implementation
- Solved
- No attempts yet
Problem
The -th suffix of a string is the suffix that starts at the -th character of . For example, if = "abcde", the 1st suffix is "abcde" and the 4th suffix is "de".
The suffix array of lists every suffix of in lexicographic order. Each entry of the array is a suffix number, and the sorting compares the suffixes those numbers refer to. For example, if = "abca", the suffix array is (4, 1, 2, 3).
Given a suffix array of length , write a program that counts the strings whose suffix array is that array.
Because can be very large, the suffix array is built by operations. First initialize SA[i] = i, then apply operations in the given order. All positions are counted from 1.
0 u v: move the -th through -th entries of to the front of the array. After this operation the suffix array isSA[u], SA[u+1], ..., SA[v], SA[1], SA[2], ..., SA[u-1], SA[v+1], ..., SA[N].1 u v: reverse the -th through -th entries of . After this operation the suffix array isSA[1], SA[2], ..., SA[u-1], SA[v], SA[v-1], ..., SA[u+1], SA[u], SA[v+1], ..., SA[N].
In this problem a string is an array of positive integers. The number of distinct integers in the string must equal the largest integer in the string. For example, (1, 2, 2) and (4, 5, 1, 2, 3) are valid strings, while (1, 1, 3) and (-1, 0, 1, 2) are not.
Input
The first line contains the length of the string () and the number of operations (). Each of the next lines contains one operation that builds the suffix array. Every operation satisfies .
Output
Print the number of strings whose suffix array is the given in the input, modulo .
Hint
In the first example the suffix array starts as (1, 2, 3, 4). After the first operation it is (1, 3, 2, 4), and after the second operation it is (2, 4, 1, 3). The possible strings are (2, 1, 2, 2), (2, 1, 3, 2), (3, 1, 3, 2), (3, 1, 4, 2).