#include <bits/stdc++.h>
using ll = long long;
const int C = 1000001, M = 1000000007;
void supfactor(int* pr, int v) {
  int i, j;
  pr[1] = 1;
  for (i = 2; i * i < v; i++) {
    if (pr[i] == 0) {
      for (j = i; j < v; j += i) {
        if (pr[j] == 0) pr[j] = i;
      }
    }
  }
  for (; i < v; i++)
    if (pr[i] == 0) pr[i] = i;
}
void dissolve(int* val, int v) {
  int i, j;
  for (i = 1; i < v; i++) val[i] = 1;
  for (i = 2; i < v; i++) {
    if (val[i] == 1) {
      for (j = i; j < v; j += i) val[j] *= i;
    }
  }
}
int a[C], primes[C], pattern[C], ambiguous[C], val[C], prime_factors[C],
    reduced[C], reduced_elements[C], dead[C];
ll fac[C];
int main() {
  int n, i = 0, j = 0, ij, elem, dream, unique = 1;
  ll x, res = 1;
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d", &a[i]);
  fac[0] = 1;
  for (x = 1; x <= n; x++) fac[x] = (fac[x - 1] * x) % M;
  for (i = 0; i <= n; i++) val[i] = 1;
  supfactor(primes, C);
  dissolve(reduced, n + 1);
  for (i = 1; i <= n; i++) reduced_elements[reduced[i]]++;
  for (i = 2; i <= n; i++) {
    if (primes[i] == i) {
      if (j != 0) {
        if (n / j != 1 && n / j != n / i && unique == 1) {
          for (ij = j; ij <= n; ij += j) val[ij] *= j;
        } else {
          for (ij = j; ij <= n; ij += j) ambiguous[ij] = 1, pattern[ij] = j;
          unique = 0;
        }
      }
      prime_factors[n / i]++;
      j = i;
    }
  }
  if (n / j != 1 && unique == 1) {
    for (ij = j; ij <= n; ij += j) val[ij] *= j;
  } else {
    for (ij = j; ij <= n; ij += j) ambiguous[ij] = 1, pattern[ij] = j;
  }
  prime_factors[1]++;
  ambiguous[1] = 1, pattern[1] = n;
  for (i = 0; i < n; i++) {
    j = i + 1;
    if (a[i] == 0) continue;
    a[i] = reduced[a[i]];
    reduced_elements[a[i]]--;
    dream = a[i] / val[j];
    if (a[i] == 1 &&
        (j == 1 || ambiguous[j] == 1 && primes[j] == j && j * 2 > n))
      ambiguous[j] = pattern[j] = 0, prime_factors[1]--;
    else if (a[i] == 1)
      res = 0;
    else if (j == 1 && (a[i] == 1 || primes[a[i]] == a[i] && a[i] * 2 > n))
      ambiguous[j] = pattern[j] = 0, val[j] *= a[i], prime_factors[1]--;
    else if (j == 1)
      res = 0;
    else if (ambiguous[j] == 1 && a[i] == val[j])
      res = 0;
    else if (dream > 1 && dead[dream] == 1)
      res = 0;
    else if (a[i] != val[j] &&
             (ambiguous[j] == 0 ||
              ambiguous[j] == 1 &&
                  (a[i] % val[j] != 0 || n / pattern[j] != n / dream ||
                   primes[dream] != dream)))
      res = 0;
    else if (ambiguous[j] == 1) {
      dead[dream] = 1;
      elem = pattern[j];
      prime_factors[n / elem]--;
      for (ij = elem; ij <= n; ij += elem) {
        val[ij] *= dream;
        ambiguous[ij] = pattern[ij] = 0;
      }
    }
  }
  for (i = 1; i <= n; i++) res = (res * fac[reduced_elements[i]]) % M;
  for (i = 1; i <= n; i++) res = (res * fac[prime_factors[i]]) % M;
  printf("%lld\n", res);
  return 0;
}
