Mex Permutations#231

Mex Permutations

1 second
256 megabytes
Hard

Initially, there is an array of integers. It is known, that all its elements are in range [0;n-1] and they are pairwise distinct. In other words, is a permutation.

Let's denote the minimal non-negative integer that does not belong to the set . For example, mex(\{1,2,3\}) = , mex(\{0,1,3\}) = , mex(\{0,1,2\}) = .

Array is being built in such a way, that F_i = mex (a_1, a_2, \dots, a_{i-1}, a_i). Let's denote = .

It is quite easy to build array by given array , but unfortunately, array was lost. You wanted to restore it using array , but there appeared to be too many possible arrays. You want to know how many arrays exists, such that = modulo .

Input

The first line of input contains one integer (1 \le n \le 10^6) — length of the permutation.

The second line of input contains integers F_i (0 \le F_i \le n+1) — elements of array .

Output

Print one integer — number of arrays that satisfy = modulo .

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Example 3

Loading
Loading

Scoring

  1. ( points): F_i = n+1;

  2. ( points): F_i < F_{i+1} for all i < n;

  3. ( points): n \le 3;

  4. ( points): n \le 7;

  5. ( points): n \le 18;

  6. ( points): n \le 1000;

  7. ( points): no additional restrictions.