#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const int mod = 1e9 + 7;
int n, p[N], vst[N], H[N], mx[N], a[N], us[N], cnt, fp[N], jc[N], pf[N];
void pre(int n) {
  jc[0] = 1;
  cnt = 0;
  memset(vst, 0, sizeof(vst));
  for (int i = 1; i <= n; i++) jc[i] = 1ll * jc[i - 1] * i % mod;
  for (int i = 2; i <= n; i++) {
    if (!vst[i]) p[++cnt] = i, mx[i] = i, us[i] = i;
    for (int j = 1; j <= cnt && i * p[j] <= n; j++) {
      vst[i * p[j]] = 1;
      if (i % p[j] == 0) {
        mx[i * p[j]] = mx[i];
        us[i * p[j]] = us[i];
        break;
      }
      mx[i * p[j]] = max(mx[i], p[j]);
      us[i * p[j]] = us[i] * p[j];
    }
  }
  mx[1] = 1;
}
int b[N];
bool g[N];
int solve() {
  memset(g, 0, sizeof(g));
  memset(b, 0, sizeof(b));
  memset(H, 0, sizeof(H));
  memset(fp, 0, sizeof(fp));
  for (int i = 1; i <= n; i++) b[us[i]]++;
  for (int i = 1; i <= cnt; i++) H[n / p[i]]++;
  H[1]++;
  for (int i = 1; i <= n; i++) {
    if (a[i]) {
      if (a[i] == 1 || i == 1) {
        if (a[i] == 1)
          H[1]--;
        else {
          b[us[a[i]]]--;
          H[n / mx[a[i]]]--;
        }
        if (a[i] != 1 && n / mx[a[i]] != 1) return 0;
        if (i != 1 && n / mx[i] != 1) return 0;
      } else {
        if (us[i] / mx[i] != us[a[i]] / mx[a[i]]) {
          return 0;
        }
        if (n / mx[i] != n / mx[a[i]]) {
          return 0;
        }
        if (fp[mx[i]] && fp[mx[i]] != mx[a[i]] ||
            pf[mx[a[i]]] && pf[mx[a[i]]] != mx[i]) {
          return 0;
        }
        fp[mx[i]] = mx[a[i]];
        pf[mx[a[i]]] = mx[i];
        b[us[a[i]]]--;
        if (!g[mx[a[i]]]) {
          g[mx[a[i]]] = 1;
          H[n / mx[a[i]]]--;
        }
      }
    }
  }
  int res = 1;
  for (int i = 1; i <= n; i++) res = 1ll * res * jc[b[i]] % mod;
  for (int i = 1; i <= n; i++) res = 1ll * res * jc[H[i]] % mod;
  return res;
}
int main() {
  int T;
  T = 1;
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    pre(n);
    printf("%d\n", solve());
  }
  return 0;
}
