#include <bits/stdc++.h>
using namespace std;
int const oo = 1e9, bound = 1e6, mod = oo + 7;
long long const OO = 1e18;
int adj[bound];
int disc[bound], low[bound];
bool isIn[bound];
bool vis[bound];
stack<int> st;
int cc = 0;
int SCC[bound];
void dfs(int u) {
  static int tim = 0;
  vis[u] = 1;
  st.push(u);
  isIn[u] = 1;
  disc[u] = low[u] = ++tim;
  if (!vis[adj[u]]) dfs(adj[u]);
  if (isIn[adj[u]]) low[u] = min(low[u], low[adj[u]]);
  if (disc[u] == low[u]) {
    int in = 0;
    while (st.top() != u) {
      isIn[st.top()] = 0;
      st.pop();
      in++;
    }
    in++;
    isIn[u] = 0;
    st.pop();
    SCC[cc] = in;
    cc++;
  }
}
long long pw(int n, int p) {
  if (p == 0) return 1;
  if (p == 1) return n;
  long long res = pw(n, p / 2);
  res = (res * res) % mod;
  if (p % 2) res = (res * 2) % mod;
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int(i) = 1; (i) <= (n); (i)++) {
    int a;
    scanf("%d", &a);
    adj[i] = a;
  }
  for (int(i) = 1; (i) <= (n); (i)++)
    if (!vis[i]) dfs(i);
  long long res = 1;
  for (int(i) = 0; (i) < (cc); (i)++) {
    if (SCC[i] == 0) continue;
    if (SCC[i] > 1)
      res = (res * (pw(2, SCC[i]) - 2)) % mod;
    else
      res = (res * 2) % mod;
  }
  printf("%lld\n", res);
  return 0;
}
