This page is still under construction.

Parts of this page are still being built. What you see may change.

Gondola replacement count

Time limit1sMemory limit256 MB

Summary
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 nn gondolas numbered 1 through nn that move in a single direction. After gondola ii passes the station, gondola i+1i+1 passes next, and after nn comes 1 again.

When a gondola breaks, it is replaced at the same position by a spare. Spares are numbered n+1,n+2,…n+1, n+2, \ldots and the smallest unused spare is always chosen. For example, with n=5n=5, if gondola 1 breaks, spare 6 takes its place.

A gondola sequence lists the nn 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 n=5n=5, both (2,3,4,5,1)(2,3,4,5,1) and (4,5,1,2,3)(4,5,1,2,3) are possible, but (4,3,2,5,1)(4,3,2,5,1) is not.

A replacement sequence lists the numbers of gondolas that broke, in breakdown order. Replacement sequence ee creates gondola sequence gg if, starting from the initial setup, after all breakdowns and replacements described by ee, gg is a possible gondola sequence.

Given a sequence of length nn, output the number of replacement sequences that could create it, modulo 1 000 000 0091\,000\,000\,009. 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 nn.

The second line contains the nn integers of the sequence.

Output

Print one integer, the count modulo 1 000 000 0091\,000\,000\,009.

Examples1

  1. Example 1

    Input
    4
    1 2 7 6
    
    Expected output
    2