Coprime Permutation

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:

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 + 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 109 + 7 (i.e. modulo 1000000007).

Examples

input

4
0 0 0 0

output

4

input

5
0 0 1 2 0

output

2

input

6
0 0 1 2 0 0

output

0

input

5
5 3 4 2 1

output

0

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).

Solution:

#include <bits/stdc++.h>

using namespace std;

const int md = 1000000007;

inline int mul(int a, int b) {
return (long long) a * b % md;
}

void error() {
puts("0");
exit(0);
}

const int N = 1234567;

int p[N], fact[N], group[N], pr[N];
vector <int> d[N];
int link_a[N], link_b[N];
int zeros[N];
int aux[N];

int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
    scanf("%d", p + i);
  }
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  for (int i = 1; i <= n; i++) {
    group[i] = n / i;
  }
  group[1] = 1;
  for (int i = 1; i <= n; i++) {
    pr[i] = i;
  }
  for (int i = 2; i <= n; i++) {
    if (pr[i] == i) {
      for (int j = i + i; j <= n; j += i) {
        if (pr[j] == j) {
          pr[j] = i;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    int tmp = i;
    d[i].clear();
    while (tmp > 1) {
int num = pr[tmp];
d[i].push_back(num);
while (tmp % num == 0) {
tmp /= num;
}
}
}
d[1].push_back(1);
for (int i = 1; i <= n; i++) {
    link_a[i] = 0;
    link_b[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] != 0) {
      int si = d[i].size();
      int spi = d[p[i]].size();
      if (si != spi) {
        error();
      }
      for (int j = 0; j < si; j++) {
        int x = d[i][j];
        int y = d[p[i]][j];
        if (group[x] != group[y]) {
          error();
        }
        if (link_a[x] == 0 && link_b[y] == 0) {
          link_a[x] = y;
          link_b[y] = x;
          continue;
        }
        if (link_a[x] != y || link_b[y] != x) {
          error();
        }
      }
    }
  }
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    zeros[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (pr[i] == i && link_a[i] == 0) {
      zeros[group[i]]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (zeros[i] > 0) {
ans = mul(ans, fact[zeros[i]]);
}
}
for (int i = 1; i <= n; i++) {
    aux[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] != 0) {
      continue;
    }
    int h = 1;
    for (int j = 0; j < (int) d[i].size(); j++) {
      h *= d[i][j];
    }
    aux[h]++;
  }
  for (int i = 1; i <= n; i++) {
    if (aux[i] > 0) {
ans = mul(ans, fact[aux[i]]);
}
}
printf("%d\n", ans);
return 0;
}