#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, F = 1e6;
const int mod = 1e9 + 7;
int n, a[N], c[N], fac[N];
int p[F / 3], pn, id[F];
bool isnp[F + 5];
inline void sieve() {
  isnp[0] = isnp[1] = 1;
  for (int i = 2; i <= F; ++i) {
    if (!isnp[i]) p[++pn] = i, id[i] = pn;
    for (int j = 1; j <= pn && i * p[j] <= F; ++j) {
      isnp[i * p[j]] = 1;
      if (i % p[j] == 0) break;
    }
  }
}
vector<int> y[N];
inline void divide() {
  y[1].push_back(1);
  for (int i = 1; i <= pn; ++i)
    for (int j = p[i]; j <= F; j += p[i]) y[j].push_back(p[i]);
}
inline void combine(int x) {
  c[x] = 1;
  for (int i = 0; i < y[x].size(); ++i) c[x] = c[x] * y[x][i];
}
int t[N], u[N], dot[N], fr[N], rf[N];
inline void sol() {
  int ans = 1;
  cin >> n;
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  for (int i = 1; i <= n; ++i)
    t[i] = 0, dot[i] = 0, u[i] = 0, rf[i] = fr[i] = 0;
  for (int i = 1; i <= n; ++i) t[c[i]]++;
  for (int i = 1; i <= n; ++i)
    if (a[i] != 0) t[c[i]]--;
  for (int i = 1; i <= n; ++i) ans = 1ll * ans * fac[t[i]] % mod;
  for (int i = 1; i <= n; ++i) t[i] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j < y[i].size(); ++j) t[id[y[i][j]]]++;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != 0) {
      if (y[i].size() != y[a[i]].size()) {
        puts("0");
        return;
      }
      for (int j = 0; j < y[i].size(); ++j) {
        int ta = y[i][j], tb = y[a[i]][j];
        ta = id[ta], tb = id[tb];
        if (t[ta] != t[tb]) {
          puts("0");
          return;
        }
        if (fr[ta] && fr[ta] != tb) {
          puts("0");
          return;
        }
        fr[ta] = tb;
        if (rf[tb] && rf[tb] != ta) {
          puts("0");
          return;
        }
        rf[tb] = ta;
      }
    }
  }
  for (int i = 1; i <= pn; ++i)
    if (!fr[i]) u[t[i]]++;
  for (int i = 1; i <= n; ++i) ans = 1ll * ans * fac[u[i]] % mod;
  cout << ans << endl;
}
int main() {
  sieve();
  fac[0] = 1;
  for (int i = 1; i <= F; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;
  divide();
  for (int i = 1; i <= F; ++i) combine(i);
  isnp[1] = 0;
  p[++pn] = 1;
  id[1] = pn;
  sol();
  return 0;
}
