#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int sum = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  if (f) return -sum;
  return sum;
}
inline void ex() {
  puts("0");
  exit(0);
}
const int N = 1000005;
const int mod = 1e9 + 7;
vector<int> P[N];
int n, lb[N], cs[N], B1[N], B2[N];
int jc[N], in[N], out[N];
int main() {
  int i, j, x, y, a, b;
  n = read();
  jc[0] = 1;
  for (i = 1; i <= n; i++) lb[i] = 1;
  for (i = 1; i <= n; i++) jc[i] = 1LL * jc[i - 1] * i % mod;
  for (i = 2; i <= n; i++)
    if (!P[i].size()) {
      cs[i] = n / i;
      B1[n / i]++;
      for (j = i; j <= n; j += i) P[j].push_back(i), lb[j] *= i;
    }
  cs[1] = 1;
  B1[1]++;
  P[1].push_back(1);
  for (i = 1; i <= n; i++) B2[lb[i]]++;
  for (i = 1; i <= n; i++) {
    x = read();
    if (!x) continue;
    B2[lb[x]]--;
    if ((y = P[i].size()) != P[x].size()) ex();
    for (j = 0, y--; j < y; j++)
      if (P[i][j] != P[x][j]) ex();
    a = P[i].back();
    b = P[x].back();
    if (cs[a] != cs[b]) ex();
    if (in[b] && in[b] != a) ex();
    if (out[a] && out[a] != b) ex();
    if (!out[a] && !in[b]) B1[cs[a]]--;
    out[a] = b;
    in[b] = a;
  }
  long long ans = 1;
  for (i = 1; i <= n; i++) ans = ans * jc[B1[i]] % mod;
  for (i = 1; i <= n; i++) ans = ans * jc[B2[i]] % mod;
  cout << ans;
  return 0;
}
