#include <bits/stdc++.h>
using namespace std;
int vis[200005], head[200005], fre, f[200005];
struct p {
  int node, next;
} E[200005];
struct pp {
  int from, to;
} e[200005];
int dfs(int st, int e) {
  vis[st] = 1;
  for (int i = head[st]; i != -1; i = E[i].next) {
    int fr = E[i].node;
    if (!vis[fr])
      return dfs(fr, e + 1);
    else
      return e + 1;
  }
}
void insertt(int x, int y) {
  E[fre] = {y, head[x]};
  head[x] = fre++;
}
int _f(int x) { return x == f[x] ? x : _f(f[x]); }
long long _fast(long long x) {
  if (x == 0) return 1;
  if (x & 1)
    return (_fast(x - 1) % 1000000007) * 2;
  else {
    long long t = _fast(x / 2);
    return (t % 1000000007) * (t % 1000000007) % 1000000007;
  }
}
int main() {
  int n, t;
  memset(head, -1, sizeof(head));
  scanf("%d", &n);
  fre = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t);
    insertt(i, t);
    e[i].from = i;
    e[i].to = t;
  }
  for (int i = 1; i <= n; i++) {
    f[i] = i;
  }
  long long ans = 1;
  long long cnt = 0;
  for (int i = 1; i <= n; i++) {
    int fx = _f(e[i].from), fy = _f(e[i].to);
    if (fx != fy)
      f[fy] = fx;
    else {
      memset(vis, 0, sizeof(vis));
      long long rec = dfs(e[i].from, 0);
      ans = (ans * (_fast(rec) - 2)) % 1000000007;
      cnt += rec;
    }
  }
  long long u = _fast(n - cnt) % 1000000007;
  cout << ans % 1000000007 * u % 1000000007 << endl;
}
