#include <bits/stdc++.h>
using namespace std;
const int p = 1e9 + 7, mn = 1000000;
int t, n, clo, isp[mn + 20], pri[mn + 20], pt, g[mn + 20], a[mn + 20],
    pp[mn + 20], tx[mn + 20], tp[mn + 20], pm[mn + 20], ci[mn + 20],
    ap[mn + 20];
long long f[mn + 20], ans;
int main() {
  pm[1] = 1;
  f[0] = 1;
  for (int i = 1; i <= mn; i++) f[i] = f[i - 1] * i % p;
  t = 1;
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) g[i] = ci[i] = 0;
    ci[1]++;
    ap[1] = 1;
    pp[1] = 1;
    g[1] = 1;
    clo++;
    pt = 0;
    for (int i = 2; i <= n; i++) {
      if (isp[i] < clo) {
        tx[i] = tp[i] = 0;
        pm[i] = i;
        pri[++pt] = i;
        ap[i] = n / i;
        ci[ap[i]]++;
        pp[i] = i;
        g[i]++;
      }
      for (int j = 1; j <= pt; j++) {
        int te = i * pri[j];
        if (te > n) break;
        isp[te] = clo;
        pm[te] = pm[i];
        if (i % pri[j] == 0) {
          pp[te] = pp[i];
          g[pp[te]]++;
          break;
        }
        pp[te] = pp[i] * pri[j];
        g[pp[te]]++;
      }
    }
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    ans = 1;
    for (int i = 1; i <= n; i++)
      if (a[i]) {
        g[pp[i]]--;
        if ((pp[i] != pp[a[i]]) && (pp[i / pm[i]] != pp[a[i] / pm[a[i]]])) {
          ans = 0;
          break;
        }
        if (ap[pm[i]] != ap[pm[a[i]]]) {
          ans = 0;
          break;
        }
        if (tx[pm[i]] && tx[pm[i]] != pm[a[i]]) {
          ans = 0;
          break;
        }
        if (tp[pm[a[i]]] && tp[pm[a[i]]] != pm[i]) {
          ans = 0;
          break;
        }
        if (!tp[pm[a[i]]] && !tx[pm[i]]) {
          ci[ap[pm[i]]]--;
        }
        tp[pm[a[i]]] = pm[i];
        tx[pm[i]] = pm[a[i]];
      }
    if (ans) {
      for (int i = 1; i <= n; i++) ans = ans * f[g[i]] % p;
      for (int i = 1; i <= n; i++) ans = ans * f[ci[i]] % p;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
