#include <bits/stdc++.h>
using namespace std;
int P[1000010];
int A[1000010];
int B[1000010];
int C[1000010];
int main() {
  ios::sync_with_stdio(false);
  int N;
  scanf("%d", &N);
  memset(P, 0, sizeof(P));
  for (int i = 1; i <= N; i++) scanf("%d", &P[i]);
  memset(A, 0, sizeof(A));
  memset(B, 0, sizeof(B));
  for (int i = 1; i <= N; i++) {
    A[i] = i;
    B[i] = 1;
    C[i] = N / i;
  }
  C[1] = 1;
  for (int i = 2; i <= N; i++) {
    if (A[i] == i) {
      for (int j = i; j <= N; j += i) {
        A[j] = i;
      }
    }
    if (B[i / A[i]] % A[i] == 0)
      B[i] = B[i / A[i]];
    else
      B[i] = A[i] * B[i / A[i]];
  }
  unordered_map<int, int> X, Y;
  for (int i = 1; i <= N; i++) {
    X[B[i]]++;
    if (A[i] == i) Y[C[i]]++;
  }
  unordered_map<int, int> M;
  unordered_map<int, int> RM;
  for (int i = 1; i <= N; i++) {
    if (P[i] != 0) {
      if (C[A[i]] != C[A[P[i]]]) {
        cout << 0 << endl;
        return 0;
      }
      int p = A[i];
      int q = A[P[i]];
      if (M.find(p) == M.end() && RM.find(q) == RM.end()) {
        Y[C[p]]--;
        M[p] = q;
        RM[q] = p;
        X[B[P[i]]]--;
      } else if (M[p] == q && RM[q] == p) {
        X[B[P[i]]]--;
      } else {
        cout << 0 << endl;
        return 0;
      }
    }
  }
  long long MOD = 1E9 + 7;
  long long F[1000010];
  F[0] = 1;
  for (long long i = 1; i <= 1000000; i++) {
    F[i] = i * F[i - 1];
    F[i] %= MOD;
  }
  unordered_map<int, int>::iterator it;
  long long res = 1;
  for (it = X.begin(); it != X.end(); ++it) {
    res *= F[it->second];
    res %= MOD;
  }
  for (it = Y.begin(); it != Y.end(); ++it) {
    res *= F[it->second];
    res %= MOD;
  }
  cout << res << endl;
}
