Codeforces Round 363 (Div. 1)
F
Coprime Permutation
Codeforces
3000
原题
← E→
翻译中…

Two positive integers are coprime if and only if they don't have a common divisor greater than $1$.

翻译中…

Some bear doesn't want to tell Radewoosh how to solve some algorithmic problem. So, Radewoosh is going to break into that bear's safe with solutions. To pass through the door, he must enter a permutation of numbers $1$ through $*n*$. The door opens if and only if an entered permutation $*p*_{1}, *p*_{2}, ..., *p*_{*n*}$ satisfies:

翻译中…

image

翻译中…

In other words, two different elements are coprime if and only if their indices are coprime.

翻译中…

Some elements of a permutation may be already fixed. In how many ways can Radewoosh fill the remaining gaps so that the door will open? Print the answer modulo $10^{9} + 7$.

翻译中…

Input

翻译中…

The first line of the input contains one integer $*n*$ ($2 ≤ *n* ≤ 1 000 000$).

翻译中…

The second line contains $*n*$ integers $*p*_{1}, *p*_{2}, ..., *p*_{*n*}$ ($0 ≤ *p*_{*i*} ≤ *n*$) where $*p*_{*i*} = 0$ means a gap to fill, and $*p*_{*i*} ≥ 1$ means a fixed number.

翻译中…

It's guaranteed that if $*i* ≠ *j*$ and $*p*_{*i*}, *p*_{*j*} ≥ 1$ then $*p*_{*i*} ≠ *p*_{*j*}$.

翻译中…

Output

翻译中…

Print the number of ways to fill the gaps modulo $10^{9} + 7$ (i.e. modulo $1000000007$).

翻译中…

Note

翻译中…

In the first sample test, none of four element is fixed. There are four permutations satisfying the given conditions: (1,2,3,4), (1,4,3,2), (3,2,1,4), (3,4,1,2).

翻译中…

In the second sample test, there must be $*p*_{3} = 1$ and $*p*_{4} = 2$. The two permutations satisfying the conditions are: (3,4,1,2,5), (5,4,1,2,3).

翻译中…

样例

Input #1
4
0 0 0 0
Output #1
4
InputCopy OutputCopy
Input #2
5
0 0 1 2 0
Output #2
2
InputCopy OutputCopy
Input #3
6
0 0 1 2 0 0
Output #3
0
InputCopy OutputCopy
Input #4
5
5 3 4 2 1
Output #4
0
InputCopy OutputCopy
题目信息
题号
698F
时间限制
2 s
内存限制
256 MB
标签
组合数学
数论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。