#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 2e5 + 10;
constexpr int MOD = 1e9 + 7;
constexpr int INF = (1 << 31);
constexpr int LOG = 30;
int mul(int a, int b) { return 1ll * a * b % MOD; }
int n, m, visited[MAXN], ans = 1, mark[MAXN], cnt;
bool Cycle = false, ok = true;
vector<int> G[MAXN];
void dfs(int u) {
  cnt++;
  if (mark[u] == -1) {
    ok = false;
  }
  visited[u] = 1;
  if (!mark[u]) {
    Cycle = false;
  }
  for (auto v : G[u]) {
    if (!visited[v]) {
      dfs(v);
    }
  }
}
int main() {
  cin >> m;
  n = 2 * m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    mark[u] = 1;
    if (u != v) {
      G[u].push_back(v);
      G[v].push_back(u);
    } else {
      mark[u] = -1;
    }
  }
  for (int i = 1; i <= n; i++) {
    Cycle = true;
    ok = true;
    cnt = 0;
    if (!visited[i] && !G[i].empty()) {
      dfs(i);
      if (Cycle && ok) {
        ans = mul(ans, 2);
      } else if (!Cycle) {
        ans = mul(ans, cnt);
      }
    }
  }
  cout << ans;
  return 0;
}
