#include <bits/stdc++.h>
void read_int(int &x) {
  int c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  x = 0;
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
}
const int MAXT = 10;
const int MAXN = 1000005;
const int MAXP = 260005;
const int MOD = 1000000007;
struct InState {
  int t;
  int n[MAXT];
  int a[MAXT][MAXN];
};
struct OutState {
  int ans[MAXT];
};
InState in;
OutState out;
void input() {
  in.t = 1;
  for (int i = 0; i < in.t; i++) {
    read_int(in.n[i]);
    for (int j = 1; j <= in.n[i]; j++) {
      read_int(in.a[i][j]);
    }
  }
}
void solve() {
  int maxn = 0;
  for (int i = 0; i < in.t; i++) {
    if (in.n[i] > maxn) {
      maxn = in.n[i];
    }
  }
  static bool flag[MAXN];
  static int minpf[MAXN];
  static int minpfpw[MAXN];
  static int primes[MAXP];
  int primen = 0;
  memset(flag + 1, 0, maxn * sizeof(*flag));
  minpf[1] = 1;
  minpfpw[1] = 1;
  for (int i = 2; i <= maxn; i++) {
    if (!flag[i]) {
      minpf[i] = i;
      minpfpw[i] = i;
      primes[primen++] = i;
    }
    for (int j = 0; j < primen && i * primes[j] <= maxn; j++) {
      flag[i * primes[j]] = true;
      minpf[i * primes[j]] = primes[j];
      if (i % primes[j]) {
        minpfpw[i * primes[j]] = primes[j];
      } else {
        minpfpw[i * primes[j]] = primes[j] * minpfpw[i];
        break;
      }
    }
  }
  static int rad[MAXN];
  rad[1] = 1;
  for (int i = 2; i <= maxn; i++) {
    rad[i] = rad[i / minpfpw[i]] * minpf[i];
  }
  static int maxpf[MAXN];
  maxpf[1] = 1;
  for (int i = 2; i <= maxn; i++) {
    if (minpfpw[i] == i) {
      maxpf[i] = minpf[i];
    } else {
      maxpf[i] = maxpf[i / minpfpw[i]];
    }
  }
  static int fac[MAXN];
  fac[0] = 1;
  for (int i = 1; i <= maxn; i++) {
    fac[i] = (long long)fac[i - 1] * i % MOD;
  }
  for (int z = 0; z < in.t; z++) {
    int n = in.n[z];
    const int *a = in.a[z];
    static int w[MAXN];
    memcpy(w + 1, rad + 1, n * sizeof(*w));
    for (int i = 0, p = 1; i < primen && primes[i] <= n; i++) {
      if (n / primes[i] != n / p) {
        p = primes[i];
      }
      w[primes[i]] = 2 * p > n ? 1 : p;
    }
    for (int i = 2; i <= n; i++) {
      if (flag[i] && n / maxpf[i] < maxpf[i]) {
        w[i] = rad[i / maxpf[i]] * w[maxpf[i]];
      }
    }
    static int pos[MAXN];
    static int invpos[MAXN];
    memset(pos + 1, 0, n * sizeof(*pos));
    memset(invpos + 1, 0, n * sizeof(*invpos));
    bool ok = true;
    for (int i = 1; i <= n; i++) {
      if (!a[i]) {
        continue;
      }
      if (w[a[i]] != w[i]) {
        ok = false;
        break;
      }
      int u = maxpf[i];
      int v = maxpf[a[i]];
      if (pos[u] && pos[u] != v) {
        ok = false;
        break;
      }
      pos[u] = v;
      if (invpos[v] && invpos[v] != u) {
        ok = false;
        break;
      }
      invpos[v] = u;
    }
    if (!ok) {
      out.ans[z] = 0;
      continue;
    }
    static int cnt1[MAXN];
    memset(cnt1 + 1, 0, n * sizeof(*cnt1));
    for (int i = 1; i <= n; i++) {
      if (!a[i]) {
        cnt1[rad[i]]++;
      }
    }
    static int cnt2[MAXN];
    memset(cnt2 + 1, 0, n * sizeof(*cnt2));
    for (int i = 1; i <= n; i++) {
      if (!flag[i] && !pos[i]) {
        cnt2[w[i]]++;
      }
    }
    int ans = 1;
    for (int i = 1; i <= n; i++) {
      ans = (long long)ans * fac[cnt1[i]] % MOD;
      ans = (long long)ans * fac[cnt2[i]] % MOD;
    }
    out.ans[z] = ans;
  }
}
void output() {
  for (int i = 0; i < in.t; i++) {
    printf("%d\n", out.ans[i]);
  }
}
int main() {
  input();
  solve();
  output();
}
