This page is still under construction.

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

Mindol Tour

Time limit1sMemory limit512 MB

Summary
Count Hamiltonian cycles starting and ending at trampoline 0 in a graph where node 0 connects to all and node i reaches nodes within distance A_i, with A_i at least i.
Level

Hard8 of 10

Topics
Dynamic programming, Combinatorics, Greedy, Math
Solved
No attempts yet

Problem

Mindol is the best rapper in the world. With the money he earned on a tour of 100 countries, he built an amusement park called Mindol Park a while ago.

The main ride in Mindol Park is a row of trampolines. There are N+1N+1 trampolines numbered 00 to NN, placed in that order, and you play by jumping between them. Trampoline 00 is the starting point, so from it you can jump to every other trampoline in one jump. From trampoline ii with 1≤i≤N1 \le i \le N you can jump in one jump only to a trampoline whose number differs by at most A_iA\_i, that is, to trampoline jj with ∣i−j∣≤A_i|i - j| \le A\_i. For safety, every trampoline can reach trampoline 00 in one jump, so A_i≥iA\_i \ge i holds.

Minsol, a fan of Mindol, went to Mindol Park and decided to try a Mindol tour, named after the Hamiltonian tour. A Mindol tour starts at trampoline 00, visits each of the other NN trampolines exactly once, and returns to trampoline 00. Tours with a different visiting order are different tours, so a tour and its reverse are counted separately.

Minsol wants to make every possible Mindol tour once before going home. Count how many times she rides.

Input

The first line contains the number of trampolines other than trampoline 00, NN (1≤N≤200 0001 \le N \le 200\,000).

The second line contains A_1,A_2,…,A_NA\_1, A\_2, \dots, A\_N (i≤A_i≤Ni \le A\_i \le N), separated by spaces.

Output

Print the number of possible Mindol tours modulo 109+710^9+7 on the first line.

Note

For N=3N = 3 and A=(1,3,3)A = (1, 3, 3) the possible tours are 0→1→2→3→00 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 0, 0→2→3→1→00 \rightarrow 2 \rightarrow 3 \rightarrow 1 \rightarrow 0, 0→3→1→2→00 \rightarrow 3 \rightarrow 1 \rightarrow 2 \rightarrow 0, and 0→3→2→1→00 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 0.

Examples2

  1. Example 1

    Input
    3
    1 3 3
    
    Expected output
    4
    
  2. Example 2

    Input
    5
    5 5 5 5 5
    
    Expected output
    120