#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
inline long long mod(long long n, long long m) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long exp(long long a, long long b, long long m) {
  if (b == 0) return 1;
  if (b == 1) return mod(a, m);
  long long k = mod(exp(a, b / 2, m), m);
  if (b & 1) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
long long pot[200100];
long long sai[200100];
long long cycle[200100];
long long vis[200100];
long long cur = 0;
void put(long long u) {
  cycle[u] = cur;
  long long v = sai[u];
  vis[u] = 3;
  if (vis[v] == 3) return;
  put(v);
}
void dfs(long long u) {
  vis[u] = 2;
  long long v = sai[u];
  if (vis[v] == 0) {
    dfs(v);
  } else if (vis[v] == 2) {
    put(v);
    cur++;
    return;
  }
  vis[u] = 1;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  pot[0] = 1;
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    pot[i] = (2LL * pot[i - 1]) % 1000000007;
  }
  for (long long i = 1; i <= n; i++) {
    cin >> sai[i];
  }
  long long ans = 1;
  memset(cycle, -1, sizeof(cycle));
  for (long long i = 1; i <= n; i++) {
    if (vis[i] == 0) dfs(i);
  }
  map<long long, long long> mapa;
  for (long long i = 1; i <= n; i++) {
    if (cycle[i] != -1) {
      mapa[cycle[i]]++;
    }
  }
  long long fora = n;
  for (map<long long, long long>::iterator it = mapa.begin(); it != mapa.end();
       it++) {
    long long x = it->second;
    fora -= x;
    ans = mod(ans * (pot[x] - 2), 1000000007);
  }
  ans = mod(ans * pot[fora], 1000000007);
  cout << ans << "\n";
}
