#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct edge {
  int to, nxt;
};
vector<edge> g;
int head[200050], ecnt;
inline void addedge(int u, int v) {
  g.push_back((edge){v, head[u]});
  head[u] = g.size() - 1;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int N;
int tim, cnt, dis[200050], vis[200050];
long long ans = 1;
void DFS(int u) {
  for (int i = head[u]; ~i; i = g[i].nxt) {
    int v = g[i].to;
    if (vis[v] == tim) {
      cnt += dis[u] - dis[v] + 1;
      ans = ans * (power(2, dis[u] - dis[v] + 1) - 2) % mod;
    } else if (!vis[v]) {
      dis[v] = dis[u] + 1;
      vis[v] = tim;
      DFS(v);
    }
  }
}
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int v;
    scanf("%d", &v);
    addedge(i, v);
  }
  for (int i = 1; i <= N; i++)
    if (!dis[i]) {
      tim++;
      dis[i] = 1, vis[i] = tim;
      DFS(i);
    }
  ans = ans * power(2, N - cnt) % mod;
  printf("%lld\n", (ans + mod) % mod);
  return 0;
}
