#include <bits/stdc++.h>
using namespace std;
const int N = 1001000, P = 1000000007;
int i, j, k, n, m, ch, nm, a, t1, t2, ans, nn;
int f[N], g1[N], g2[N], h1[N], h2[N], s[N], p1[N], p2[N], pr[N / 10], Jc[N];
void R(int &x) {
  x = 0;
  ch = getchar();
  while (ch < '0' || '9' < ch) ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
}
void pre(int n) {
  int i, j, k;
  Jc[0] = 1;
  for (i = 1; i <= n; i++) Jc[i] = (long long)Jc[i - 1] * i % P;
  f[1] = 1;
  for (i = 2; i <= n; i++) {
    if (!f[i]) {
      f[i] = i;
      pr[++nm] = i;
    }
    for (j = 1; j <= nm; j++) {
      if (i * pr[j] > n) break;
      f[i * pr[j]] = pr[j];
      if (pr[j] == f[i]) break;
    }
  }
  g2[1] = 1;
  for (i = 1; i <= n; i++) {
    g1[i] = 1;
    j = i;
    while (j > 1) {
      g1[i] *= f[j];
      g2[i] = f[j];
      while (j % g2[i] == 0) j /= g2[i];
    }
  }
  for (i = 1; i <= n; i++) h1[g1[i]]++;
  h2[1] = 1;
  for (i = 2; i <= n; i++)
    if (f[i] == i) h2[i] = n / i;
  for (i = 1; i <= n; i++) s[h2[i]]++;
}
int main() {
  R(n);
  nn = 1;
  while (nn * nn <= n) nn++;
  pre(n);
  for (i = 1; i <= n; i++) {
    R(a);
    if (!a) continue;
    if ((1 < g2[g1[i]] && g2[g1[i]] < nn) ^ (1 < g2[g1[a]] && g2[g1[a]] < nn))
      return puts("0"), 0;
    if (1 < g2[g1[i]] && g2[g1[i]] < nn && 1 < g2[g1[a]] && g2[g1[a]] < nn) {
      if (g1[i] != g1[a]) return puts("0"), 0;
    } else {
      if (h2[g2[g1[i]]] != h2[g2[g1[a]]]) return puts("0"), 0;
      if (p1[g2[g1[i]]] && p1[g2[g1[i]]] != g2[g1[a]]) return puts("0"), 0;
      if (p2[g2[g1[a]]] && p2[g2[g1[a]]] != g2[g1[i]]) return puts("0"), 0;
      if (!p1[g2[g1[i]]]) {
        p1[g2[g1[i]]] = g2[g1[a]];
        p2[g2[g1[a]]] = g2[g1[i]];
        s[h2[g2[g1[i]]]]--;
      }
    }
    h1[g1[a]]--;
  }
  ans = 1;
  for (i = 1; i <= n; i++)
    if (h1[i]) ans = (long long)ans * Jc[h1[i]] % P;
  for (i = 1; i <= n; i++)
    if (s[i]) ans = (long long)ans * Jc[s[i]] % P;
  printf("%d\n", ans);
}
