CF698F.Coprime Permutation

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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 11 through nn . The door opens if and only if an entered permutation p1,p2,...,pnp_{1},p_{2},...,p_{n} satisfies:

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 109+710^{9}+7 .

输入格式

The first line of the input contains one integer nn ( 2<=n<=10000002<=n<=1000000 ).

The second line contains nn integers p1,p2,...,pnp_{1},p_{2},...,p_{n} ( 0<=pi<=n0<=p_{i}<=n ) where pi=0p_{i}=0 means a gap to fill, and pi>=1p_{i}>=1 means a fixed number.

It's guaranteed that if iji≠j and pi,pj>=1p_{i},p_{j}>=1 then pipjp_{i}≠p_{j} .

输出格式

Print the number of ways to fill the gaps modulo 109+710^{9}+7 (i.e. modulo 10000000071000000007 ).

输入输出样例

  • 输入#1

    4
    0 0 0 0
    

    输出#1

    4
    
  • 输入#2

    5
    0 0 1 2 0
    

    输出#2

    2
    
  • 输入#3

    6
    0 0 1 2 0 0
    

    输出#3

    0
    
  • 输入#4

    5
    5 3 4 2 1
    

    输出#4

    0
    

说明/提示

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 p3=1p_{3}=1 and p4=2p_{4}=2 . The two permutations satisfying the conditions are: (3,4,1,2,5), (5,4,1,2,3).

首页