#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, mod = 1e9 + 7;
int a[N], ans[N], cnt[N], group[N], fact[N], big[N];
int match[N], rmatch[N];
bool np[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  ans[1] = 1;
  big[1] = 1;
  cnt[1]++;
  group[1]++;
  for (int i = 2; i <= n; i++) {
    if (!np[i]) {
      ans[i] = i;
      big[i] = i;
      group[n / i]++;
    }
    if (!np[i]) {
      for (int j = 2 * i; j <= n; j += i) {
        np[j] = true;
        if (ans[j] == 0)
          ans[j] = i;
        else
          ans[j] *= i;
        big[j] = i;
      }
    }
    cnt[ans[i]]++;
  }
  memset(match, -1, sizeof match);
  memset(rmatch, -1, sizeof rmatch);
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0) continue;
    if (ans[i] / big[i] != ans[a[i]] / big[a[i]]) return cout << 0 << '\n', 0;
    int p1 = big[i], p2 = big[a[i]];
    int t1, t2;
    if (p1 != 1)
      t1 = n / p1;
    else
      t1 = 1;
    if (p2 != 1)
      t2 = n / p2;
    else
      t2 = 1;
    if (t1 != t2) return cout << 0 << '\n', 0;
    if (match[p1] != -1 && match[p1] != p2) return cout << 0 << '\n', 0;
    if (rmatch[p2] != -1 && rmatch[p2] != p1) return cout << 0 << '\n', 0;
    if (match[p1] != p2) group[t1]--;
    match[p1] = p2;
    rmatch[p2] = p1;
    cnt[ans[a[i]]]--;
  }
  fact[0] = 1;
  for (int i = 1; i <= n; i++) fact[i] = (1LL * fact[i - 1] * i) % mod;
  int ans = 1;
  for (int i = 1; i <= n; i++) ans = (1LL * ans * fact[cnt[i]]) % mod;
  for (int i = 1; i <= n; i++) ans = (1LL * ans * fact[group[i]]) % mod;
  printf("%d\n", ans);
  return 0;
}
