#include <bits/stdc++.h>
inline void in(int &MAGNUS) { scanf("%d", &MAGNUS); }
inline void out(int MAGNUS) { printf("%d\n", MAGNUS); }
inline void in(int &MAGNUS, int &CLAR) { scanf("%d%d", &MAGNUS, &CLAR); }
inline void out(int MAGNUS, int CLAR) { printf("%d %d\n", MAGNUS, CLAR); }
inline void inl(long long &LIV) { scanf("%lld", &LIV); }
inline void inl(long long &LIV, long long &MART) {
  scanf("%lld%lld", &LIV, &MART);
}
inline void outl(long long LIV) { printf("%lld\n", LIV); }
inline void outl(long long LIV, long long MART) {
  printf("%lld %lld\n", LIV, MART);
}
using namespace std;
short mark[200005];
int nex[200005];
long long powersets[200005];
bool adacycle = 0;
vector<int> ccsize;
void findcycle(int u) {
  ccsize.back()++;
  mark[u] = 3;
  if (mark[nex[u]] == 3) return;
  findcycle(nex[u]);
}
void dfs(int u) {
  mark[u] = 2;
  int v = nex[u];
  if (mark[v] == 0) {
    dfs(v);
  } else if (mark[v] == 1) {
    mark[u] = 1;
    return;
  } else {
    ccsize.push_back(0);
    findcycle(u);
  }
  mark[u] = 1;
}
int main() {
  int N;
  in(N);
  for (int i = 1; i <= N; i++) in(nex[i]);
  powersets[0] = 1LL;
  for (int i = 1; i <= 200000; i++) {
    powersets[i] = powersets[i - 1] * 2LL;
    powersets[i] %= 1000000007;
  }
  long long ans = 1LL;
  int edge = N;
  for (int i = 1; i <= N; i++) {
    if (!mark[i]) dfs(i);
  }
  for (int i = 0; i <= (int)ccsize.size() - 1; i++) {
    ans *= (powersets[ccsize[i]] - 2);
    ans %= 1000000007;
    edge -= ccsize[i];
  }
  ans *= powersets[edge];
  ans %= 1000000007;
  outl(ans);
  return 0;
}
