#include <bits/stdc++.h>
using namespace std;
int N;
int Array[1000005];
int ans;
const int MOD = 1000000007;
int P[1000005], Prime[1000005], rep[1000005], nb[1000005], nb2[1000005],
    ct[1000005];
int cnt;
vector<int> V[1000005];
int rev[1000005], Fact[1000005];
int Assign[1000005];
void Eratostenes() {
  for (int i = 1; i <= N; i++) rep[i] = 1;
  for (int i = 2; i <= N; i++) {
    if (P[i] == 0) {
      Prime[++cnt] = i;
      rep[i] = i;
      V[i].push_back(i);
      for (int j = i + i; j <= N; j += i) {
        P[j] = 1;
        V[j].push_back(i);
        rep[j] *= i;
      }
    }
  }
}
void Read() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) scanf("%d", &Array[i]);
}
inline int get(int x) {
  if (x == 1) return 1;
  return N / x;
}
void Solve() {
  V[1].push_back(1);
  Fact[0] = 1;
  for (int i = 1; i <= N; i++) {
    Fact[i] = (1LL * Fact[i - 1] * i) % MOD;
  }
  ct[1] = 1;
  for (int i = 1; i <= cnt; i++) {
    ct[get(Prime[i])]++;
  }
  for (int i = 1; i <= N; i++) {
    if (Array[i] == 0) continue;
    int val = Array[i];
    if (V[val].size() != V[i].size()) {
      printf("0\n");
      return;
    }
    for (int j = 0; j < V[i].size(); j++) {
      int p = V[i][j];
      int q = V[val][j];
      if (get(p) != get(q)) {
        printf("0\n");
        return;
      }
      if (Assign[p] != 0 && Assign[p] != q) {
        printf("0\n");
        return;
      }
      if (rev[q] != 0 && rev[q] != p) {
        printf("0\n");
        return;
      }
      if (Assign[p] == 0) {
        Assign[p] = q;
        rev[q] = p;
        ct[get(p)]--;
      }
    }
  }
  for (int i = 1; i <= N; i++) nb2[rep[i]]++;
  for (int i = 1; i <= N; i++) {
    if (Array[i] != 0) nb2[rep[i]]--;
  }
  ans = 1;
  for (int i = 1; i <= N; i++) {
    ans = (1LL * ans * Fact[ct[i]]) % MOD;
    ans = (1LL * ans * Fact[nb2[i]]) % MOD;
  }
  printf("%d\n", ans);
}
int main() {
  Read();
  Eratostenes();
  Solve();
  return 0;
}
