#include <bits/stdc++.h>
inline void read(int& r) {
  register char c;
  r = 0;
  do c = getchar();
  while (c < '0' || c > '9');
  if (c != '0') do
      r = r * 10 + c - '0', c = getchar();
    while (c >= '0' && c <= '9');
}
const int mod = 1e9 + 7, N = 1000004;
bool is_not_prime[N];
int sf[N], d[N], p[N], top;
void init() {
  for (int i = 2; i < N; i++) {
    if (!is_not_prime[i]) p[top++] = sf[i] = d[i] = i;
    for (int j = 0; j < top && i * p[j] < N; j++) {
      d[i * p[j]] = d[i];
      is_not_prime[i * p[j]] = true;
      if (i % p[j] == 0) {
        sf[i * p[j]] = sf[i];
        break;
      }
      sf[i * p[j]] = sf[i] * p[j];
    }
  }
}
int pos[N], cnt[N], a[N], ans, n;
bool check(int x, int y) {
  x = sf[x], y = sf[y];
  if (x == 1 && d[y] == y && n / y == 1) return true;
  if (y == 1 && d[x] == x && n / x == 1) return true;
  while (x != 1 && y != 1)
    if (n / d[x] != n / d[y])
      return false;
    else
      x /= d[x], y /= d[y];
  return x == 1 && y == 1;
}
bool check(int p) {
  for (int i = p; i <= n; i += p)
    if (pos[i]) return false;
  return true;
}
int gcd(int a, int b) {
  int t;
  while (b) t = a % b, a = b, b = t;
  return a;
}
int main() {
  int i, j, t;
  sf[1] = 1, init(), read(n), ans = 1;
  for (i = 1; i <= n; i++) read(a[i]), pos[a[i]] = i;
  for (i = 2; i <= n; i++) {
    for (j = i, t = 0; j <= n; j += i) t = gcd(t, a[j]);
    if (t == 1 && (d[i] != i || n / i > 1)) return puts("0"), 0;
  }
  for (i = 2; i <= n; i++) {
    for (j = i, t = 0; j <= n; j += i) t = gcd(t, pos[j]);
    if (t == 1 && (d[i] != i || n / i > 1)) return puts("0"), 0;
  }
  for (i = 0; i < top && p[i] <= n;) {
    t = n / p[i], j = check(p[i]);
    if (t == 1 && !pos[1]) ans = (long long)ans * ++j % mod;
    for (; t == n / p[++i];)
      if (check(p[i])) ans = (long long)ans * ++j % mod;
  }
  if (pos[1] && !check(1, pos[1])) ans = 0;
  for (i = 2; i <= n && ans; i++)
    if (pos[i])
      check(i, pos[i]) || (ans = 0);
    else
      ans = (long long)ans * ++cnt[sf[i]] % mod;
  printf("%d\n", ans);
}
