#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
const int maxN = N + 7;
const long long O = 1e9 + 7;
template <class T>
T rd() {
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = 0;
  T x = 0;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return f ? x : -x;
}
int n, sn;
long long fac[maxN];
void init() {
  fac[0] = 1;
  for (int i = (1), _ = (N); i <= _; ++i) fac[i] = fac[i - 1] * i % O;
}
bool ls(int x) { return 1 < x && x <= sn; }
namespace C {
int A[maxN], B[maxN];
int go[maxN], fr[maxN];
int cnt[maxN], pp[maxN], las[maxN], occ[maxN];
long long res;
void init() {
  for (int i = (1), _ = (n); i <= _; ++i) pp[i] = 1, cnt[i] = 0, las[i] = 0;
  cnt[1] = 1;
  las[1] = 1;
  occ[1] = 1;
  for (int i = (2), _ = (n); i <= _; ++i)
    if (cnt[i] == 0) {
      occ[i] = 0;
      for (int j = i; j <= n; j += i) {
        cnt[j]++;
        pp[j] *= i;
        las[j] = i;
        ++occ[i];
      }
    }
}
void pre() {
  memset(A, 0, sizeof A);
  memset(B, 0, sizeof B);
  memset(go, 0, sizeof go);
  memset(fr, 0, sizeof fr);
  sn = sqrtl(n);
  for (int i = (1), _ = (n); i <= _; ++i) A[pp[i]]++;
  for (int i = (sn + 1), _ = (n); i <= _; ++i) B[occ[i]]++;
  ++B[1];
  res = 1;
}
void gao(int x, int y) {
  if (y == 0) return;
  if (cnt[x] != cnt[y]) {
    res = 0;
    return;
  }
  A[pp[y]]--;
  x = las[x], y = las[y];
  if ((ls(x) || ls(y)) && x != y) {
    res = 0;
    return;
  } else if (ls(x) && ls(y))
    return;
  else if (occ[x] != occ[y]) {
    res = 0;
    return;
  }
  if (go[y] == x) return;
  if (go[y]) {
    res = 0;
    return;
  }
  if (fr[x]) {
    res = 0;
    return;
  }
  go[y] = x;
  fr[x] = y;
  B[occ[y]]--;
}
long long calc() {
  for (int i = (1), _ = (n); i <= _; ++i) res = res * fac[A[i]] % O;
  for (int i = (1), _ = (n); i <= _; ++i) res = res * fac[B[i]] % O;
  return res;
}
}  // namespace C
int main() {
  init();
  n = rd<int>();
  C::init();
  C::pre();
  for (int i = (1), _ = (n); i <= _; ++i) C::gao(i, rd<int>());
  printf("%I64d\n", C::calc());
  return 0;
}
