#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
vector<int> g[N];
bitset<N> done;
bitset<N> bad, badder;
int nodes[N], edges[N];
void markbad(int u) {
  bad[u] = 1;
  for (auto v : g[u])
    if (!bad[v]) markbad(v);
}
int dfs(int u, int p) {
  ++nodes[u];
  done[u] = true;
  int ret = g[u].size();
  for (auto v : g[u]) {
    if (v == p) continue;
    if (done[v]) continue;
    ret += dfs(v, u);
    nodes[u] += nodes[v];
  }
  return ret;
}
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
    if (u == v) bad[u] = 1;
  }
  long long int ans = 1;
  for (int i = 1; i <= 2 * n; ++i)
    if (bad[i]) markbad(i);
  for (int i = 1; i <= 2 * n; ++i) {
    if (done[i] or bad[i]) continue;
    auto e = dfs(i, -1) / 2;
    if (e == nodes[i])
      ans = (ans * 2) % MOD;
    else
      ans = (ans * nodes[i]) % MOD;
  }
  cout << ans;
}
void InputSetup() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int main(void) {
  auto start = chrono::high_resolution_clock::now();
  InputSetup();
  solve();
  auto finish = chrono::high_resolution_clock::now();
  cerr << "Time elapsed: "
       << (chrono::duration<long double>(finish - start)).count() << "s\n";
}
