#FT0000107. Dãy con Kadomatsu đẹp (Beautiful Kadomatsu)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy con Kadomatsu đẹp (Beautiful Kadomatsu)
Beautiful Kadomatsu
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of the integers from through . You may choose any increasing sequence of indices, not necessarily consecutive, to form a subsequence .
A position with is a peak if . A position in the same range is a valley if .
Let be the number of peaks and the number of valleys. The subsequence is called kadomatsu-like if and only if . A subsequence of length one or two has no internal position and cannot satisfy this condition.
Count all kadomatsu-like subsequences of . Different index selections count separately, even though the chosen positions need not be consecutive. Print the count modulo .
Input
- The first line contains the integer , the permutation length.
- The second line contains exactly integers in permutation order.
Output
Print the number of kadomatsu-like subsequences, modulo .
Subtasks
- Subtask 1 (20 points): ; is a permutation of (every value occurs exactly once).
- Subtask 2 (30 points): ; is a permutation of (every value occurs exactly once).
- Subtask 3 (50 points): ; is a permutation of (every value occurs exactly once).
Examples
Example 1
Input
4
1 3 4 2
Output
4
Explanation
The qualifying subsequences are , , , and . Each length-three sequence has one peak and no valley. The length-four sequence has a single peak at and no valley. Every other subsequence has no excess of peaks. Thus the answer is 4.
Example 2
Input
1
1
Output
0
Explanation
The only non-empty subsequence is , of length one. It has no internal positions, so ; therefore is false and the answer is 0.
Example 3
Input
20
11 10 18 13 12 16 5 19 7 6 17 4 9 1 14 2 20 15 8 3
Output
431610
Explanation
For instance, has two peaks () and one valley (), hence it qualifies. Counting all selections by extending subsequences that start with an ascent and end with a descent, the cumulative qualifying counts after positions are , respectively. The final answer is 431610 (less than the modulus).