#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const long long mod = 7 + (long long)1e9;
pair<int, int> a[N];
int n;
int nxt[N + N];
bool mark[N + N], eic[N + N], eme[N + N];
char vis[N + N];
vector<int> tree[N + N], cycs, treeMods;
set<int> roots;
void dfs(int u) {
  vis[u] = 2;
  if (vis[nxt[u]] == 2) {
    int cnt = 1;
    for (int v = nxt[u]; v != u; v = nxt[v]) cnt++;
    cycs.emplace_back(cnt);
    eic[u] = 1;
  } else if (!mark[nxt[u]]) {
    roots.emplace(nxt[u]);
    eme[u] = 1;
    tree[nxt[u]].emplace_back(u);
  } else {
    if (vis[nxt[u]] == 0) dfs(nxt[u]);
    eic[u] = eic[nxt[u]];
    eme[u] = eme[nxt[u]];
    if (eme) tree[nxt[u]].emplace_back(u);
  }
  vis[u] = 1;
}
int dfsTree(int x) {
  int ret = 1;
  for (auto& child : tree[x]) ret += dfsTree(child);
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fill(nxt, nxt + N + N, 0);
  fill(mark, mark + N + N, 0);
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
    mark[a[i].first] = 1;
    nxt[a[i].first] = a[i].second;
  }
  fill(vis, vis + N + N, 0);
  for (int i = 1; i <= n + n; i++)
    if (mark[i] and !vis[i]) {
      dfs(i);
    }
  for (auto& root : roots) {
    treeMods.emplace_back(dfsTree(root));
  }
  long long ans = 1;
  for (auto& cyc : cycs) {
    if (cyc > 1) ans = (ans * 2) % mod;
  }
  for (auto& ns : treeMods) {
    ans = (ans * ns) % mod;
  }
  cout << ans << endl;
  return 0;
};
