#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200005;
long long succ[maxn];
bool vis[maxn];
bool isPartOfCycle[maxn];
bool isNormal[maxn];
long long cycleLength[maxn];
long long color[maxn];
long long posMap[maxn];
long long up[20][maxn];
long long power[20];
long long cnt = 0;
stack<long long> s;
void dfs(long long u) {
  s.push(u);
  if (vis[succ[u]]) {
    if (isPartOfCycle[succ[u]] || isNormal[succ[u]]) {
      while (!s.empty()) s.pop();
      return;
    }
    stack<long long> cycle;
    long long len = 0;
    bool flag = false;
    while (!s.empty()) {
      long long node = s.top();
      s.pop();
      cycle.push(node);
      len++;
      if (node == succ[u]) {
        flag = true;
        break;
      }
    }
    if (!flag) {
      while (!cycle.empty()) {
        cycle.pop();
      }
    }
    while (!s.empty()) {
      long long node = s.top();
      isNormal[node] = true;
      s.pop();
    }
    long long pos = 0;
    while (!cycle.empty()) {
      long long node = cycle.top();
      cycle.pop();
      isPartOfCycle[node] = true;
      color[node] = cnt;
      posMap[node] = pos;
      cycleLength[node] = len;
      pos++;
    }
    cnt++;
    return;
  }
  vis[succ[u]] = true;
  dfs(succ[u]);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  for (long long i = 0; i < maxn; i++) {
    vis[i] = false;
    isPartOfCycle[i] = false;
    cycleLength[i] = 0;
    succ[i] = 0;
    isNormal[i] = false;
    posMap[i] = 0;
    color[i] = 0;
  }
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> succ[i];
  }
  for (long long i = 1; i <= n; i++) {
    if (!vis[i]) {
      vis[i] = true;
      dfs(i);
    }
  }
  long long res = 1;
  long long mod = 1e9 + 7;
  bool vis[cnt];
  for (long long i = 0; i < cnt; i++) vis[i] = false;
  for (long long i = 1; i <= n; i++) {
    if (isPartOfCycle[i]) {
      if (!vis[color[i]]) {
        vis[color[i]] = true;
        long long f = 1;
        for (long long j = 0; j < cycleLength[i]; j++) {
          f *= 2;
          f %= mod;
        }
        f -= 2;
        if (f < 0) f += mod;
        res *= f;
        res %= mod;
      }
    } else {
      res *= 2;
      res %= mod;
    }
  }
  while (res < 0) res += mod;
  cout << res << endl;
}
