#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
int n, cnt;
int a[N], sdfsdf[N], sit[N], b[N], h[N], oncyc[N];
vector<int> v[N];
void dfs(int p, int x) {
  cnt++;
  for (auto u : v[x]) {
    if (u != p) dfs(x, u);
  }
}
void dfs2(int x) {
  sdfsdf[x] = 1;
  for (auto u : v[x]) {
    if (!sdfsdf[u]) dfs2(u);
  }
  if (sit[x] and !sdfsdf[sit[x]]) dfs2(sit[x]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", a + i, b + i);
    sit[a[i]] = b[i];
    v[b[i]].push_back(a[i]);
  }
  int mul = 1;
  for (int i = 1; i <= 2 * n; i++) {
    if (!sdfsdf[i] and sit[i] and !h[i]) {
      int x = i;
      while (x and !h[x]) {
        h[x] = 1;
        oncyc[x] = 1;
        x = sit[x];
      }
      if (x and x != sit[x]) {
        mul = (long long)mul * 2 % mod;
      }
      dfs2(i);
    }
  }
  for (int i = 1; i <= 2 * n; i++) {
    if (!sit[i] and v[i].size()) {
      cnt = 0;
      dfs(0, i);
      mul = (long long)mul * (cnt) % mod;
    }
  }
  printf("%d\n", mul);
  return 0;
}
