#include <bits/stdc++.h>
using namespace std;
const int MOD = int(1e9) + 7;
int n;
int a[100000], b[100000];
int who[200005];
bool used[200005] = {};
vector<int> g[200005];
int mul;
int far[200005];
int tr[200005];
void dfs(int v) {
  ++mul;
  used[v] = true;
  for (int to : g[v]) {
    dfs(to);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); ++i) {
    scanf("%d%d", a + i, b + i);
    --a[i];
    --b[i];
    g[b[i]].push_back(a[i]);
  }
  memset(who, -1, sizeof who);
  for (int i = 0; i < (int)(n); ++i) who[a[i]] = i;
  int ans = 1;
  for (int i = 0; i < (int)(2 * n); ++i)
    if (who[i] == -1) {
      mul = 0;
      dfs(i);
      ans = (long long)ans * mul % MOD;
    }
  memset(far, -1, sizeof far);
  for (int i = 0; i < (int)(n); ++i) far[a[i]] = b[i];
  for (int _ = 0; _ < (int)(25); ++_)
    for (int i = 0; i < (int)(2 * n); ++i) {
      if (far[i] != -1) {
        far[i] = far[far[i]];
      }
    }
  for (int i = 0; i < (int)(n); ++i) tr[a[i]] = b[i];
  for (int i = 0; i < (int)(2 * n); ++i)
    if (far[i] != -1 && !used[far[i]]) {
      int c = 0;
      for (int x = far[i]; !used[x]; x = tr[x]) {
        ++c;
        used[x] = true;
      }
      if (c > 1) {
        ans = 2ll * ans % MOD;
      }
    }
  cout << ans << endl;
  return 0;
}
