#include <bits/stdc++.h>
using namespace std;
int n, B[((long long)1e6 + 1)];
int cnt1[((long long)1e6 + 1)], cnt2[((long long)1e6 + 1)];
int pos1[((long long)1e6 + 1)], pos2[((long long)1e6 + 1)];
long long F[((long long)1e6 + 1)], ans = 1;
vector<int> p[((long long)1e6 + 1)];
void neg() {
  puts("0");
  exit(0);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cnt1[1] = F[0] = 1;
  for (int i = 1; i <= n; ++i) {
    F[i] = F[i - 1] * (long long)i % ((long long)1e9 + 7);
    B[i] = 1;
  }
  p[1].push_back(1);
  for (int i = 2; i <= n; ++i)
    if (!p[i].size()) {
      cnt1[n / i]++;
      for (int j = i; j <= n; j += i) {
        p[j].push_back(i);
        B[j] *= i;
      }
    }
  for (int i = 1; i <= n; ++i) cnt2[B[i]]++;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    if (!x) continue;
    if (p[i].size() != p[x].size()) neg();
    for (int j = 0; j < p[i].size(); ++j) {
      int a1 = p[i][j];
      int a2 = p[x][j];
      int p1 = (a1 < 2 ? 1 : n / a1);
      int p2 = (a2 < 2 ? 1 : n / a2);
      if (p1 != p2) neg();
      if (pos1[a1] && pos1[a1] != a2) neg();
      if (pos2[a2] && pos2[a2] != a1) neg();
      if (!pos1[a1]) {
        pos1[a1] = a2;
        pos2[a2] = a1;
        cnt1[p1]--;
      }
    }
    cnt2[B[i]]--;
  }
  for (int i = 1; i <= n; ++i)
    ans = ans * F[cnt1[i]] % ((long long)1e9 + 7),
    ans = ans * F[cnt2[i]] % ((long long)1e9 + 7);
  cout << ans << endl;
  return 0;
}
