Gondola replacement count
Time limit1sMemory limit256 MB
Count the replacement orders that can produce the given circular gondola sequence, modulo 1000000009.
- Level
Hard9 of 10
- Topics
- Combinatorics, Math
- Solved
- No attempts yet
Problem
The Maokong Gondola in Taipei runs on a circular rail with one station and gondolas numbered 1 through that move in a single direction. After gondola passes the station, gondola passes next, and after comes 1 again.
When a gondola breaks, it is replaced at the same position by a spare. Spares are numbered and the smallest unused spare is always chosen. For example, with , if gondola 1 breaks, spare 6 takes its place.
A gondola sequence lists the consecutive gondola numbers seen at the station starting from some moment. Breakdowns may have happened before recording starts, but none occur while the sequence is recorded.
The same physical setup can yield different gondola sequences depending on when you start recording. With no breakdowns and , both and are possible, but is not.
A replacement sequence lists the numbers of gondolas that broke, in breakdown order. Replacement sequence creates gondola sequence if, starting from the initial setup, after all breakdowns and replacements described by , is a possible gondola sequence.
Given a sequence of length , output the number of replacement sequences that could create it, modulo . Output 0 if the sequence is not a gondola sequence, and 1 if it is a gondola sequence with no breakdowns.
Input
The first line contains .
The second line contains the integers of the sequence.
Output
Print one integer, the count modulo .