#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
const long long mod = 1e9 + 7;
vector<int> g[N], comp;
int deg[N], vis[N], self[N];
void dfs(int v) {
  vis[v] = 1;
  comp.push_back(v);
  for (auto it : g[v]) {
    if (!vis[it]) {
      dfs(it);
    }
  }
}
int main() {
  long long ans = 1;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
    if (a == b) self[a] = 1;
    deg[a]++;
    deg[b]++;
  }
  for (int i = 1; i <= 2 * n; i++) {
    if (!vis[i]) {
      comp.clear();
      dfs(i);
      if ((int)comp.size() == 1) continue;
      int cnt = 0;
      bool flag = false;
      for (auto it : comp) {
        cnt += deg[it];
        if (self[it]) flag = true;
      }
      cnt /= 2;
      if (cnt == (int)comp.size()) {
        if (!flag) ans = (ans * 2) % mod;
      } else {
        ans = (ans * (int)comp.size()) % mod;
      }
    }
  }
  printf("%lld\n", ans);
}
