#include #include #include #include #include #include using namespace std; template class y_combinator_result { Fun fun_; public: template explicit y_combinator_result(T &&fun): fun_(std::forward(fun)) {} template decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward(args)...); } }; template decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result>(std::forward(fun)); } const int MOD = int(1e9) + 7; int main() { int n; scanf("%d", &n); vector to(n); vector> par(n); for(int i = 0; i < n; i++) { scanf("%d", &to[i]); par[--to[i]].push_back(i); } vector seen(n); int ret = 1; for(int i = 0; i < n; i++) { if(seen[i]) continue; int lhs = to[i]; int rhs = to[to[i]]; while(lhs != rhs) { lhs = to[lhs]; rhs = to[to[rhs]]; } int cyclesz = 1; int now = to[lhs]; seen[now] = true; while(now != lhs) { cyclesz++; now = to[now]; seen[now] = true; } int ans = 1; for(int i = 0; i < cyclesz; i++) { for(int out: par[now]) { if(!seen[out]) { auto dfs = y_combinator([&](auto self, int curr) -> int { assert(!seen[curr]); seen[curr] = true; int ret = 1; for(int out: par[curr]) { ret = (ret * (int64_t)self(out)) % MOD; } ret++; if(ret == MOD) ret = 0; return ret; }); ans = (ans * (int64_t)dfs(out)) % MOD; } } now = to[now]; } ret = (ret * (int64_t)(ans+1)) % MOD; } printf("%d\n", (ret+MOD-1)%MOD); }