#P1876D. Lexichromatography
Lexichromatography
Description
Pak Chanek loves his faculty, the Faculty of Computer Science, University of Indonesia (Fasilkom). He wants to play with the colours of the faculty's logo, blue and red.
There is an array consisting of elements, element has a value of . Pak Chanek wants to colour each element in the array blue or red such that these following conditions are satisfied:
- If all blue elements are formed into a subsequence and so are all the red elements, the blue subsequence is strictly less than the red subsequence lexicographically.
- Array does not have any subarray that is imbalanced. A subarray is imbalanced if and only if there is a value such that the absolute difference between the number of blue elements with value and the number of red elements with value in this subarray is or more.
- Note that it is possible to colour every element of the array the same colour.
How many different colourings satisfy all those conditions? Since the answer can be very big, print the answer modulo . Two colourings are different if and only if there is at least one element that is blue in one colouring, but red in the other.
A subsequence of an array is a sequence that can be obtained from the array by deleting some elements (possibly none), without changing the order of the remaining elements.
Let and be two different sequences. Sequence is said to be lexicographically less than sequence if and only if is a prefix of or there is an index such that holds for every , and . In particular, an empty sequence is always lexicographically less than any non-empty sequence.
The first line contains a single integer () — the size of array .
The second line contains integers ().
An integer representing the number of different colourings that satisfy all of the problem's conditions, modulo .
Input
The first line contains a single integer () — the size of array .
The second line contains integers ().
Output
An integer representing the number of different colourings that satisfy all of the problem's conditions, modulo .
Note
In the first example, the ways for colouring all elements from index to index are:
- Blue, red, red, blue, blue, red, red, blue.
- Blue, red, red, red, blue, blue, red, blue.
- Red, red, blue, blue, blue, red, red, blue.
As an example, if we colour the elements from index to index to be red, red, blue, red, red, blue, blue, blue, it is not valid colouring, because for subarray , there are blue elements with value and red elements with value , making subarray an imbalanced subarray.