#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<vector<int> > adj, rev;
int add(int a, int b) {
  int ret = a + b;
  if (ret >= 1000000007) ret -= 1000000007;
  return ret;
}
int mul(int a, int b) {
  long long ret = 1LL * a * b;
  if (ret >= 1000000007) ret %= 1000000007;
  return ret;
}
int visited[N];
int occ[N];
int s[N], f[N];
int nxt[N], indeg[N];
int dfs(int v) {
  int ret = 1;
  for (int i = 0; i < rev[v].size(); i++) {
    int u = rev[v][i];
    ret += dfs(u);
  }
  return ret;
}
int main() {
  int n;
  cin >> n;
  adj.resize(2 * n + 5);
  rev.resize(2 * n + 5);
  int ans = 1;
  memset(occ, 0, sizeof(occ));
  memset(nxt, -1, sizeof(nxt));
  memset(indeg, 0, sizeof(indeg));
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &s[i], &f[i]);
    occ[s[i]] = 1;
    nxt[s[i]] = f[i];
    if (s[i] != f[i]) indeg[f[i]]++;
    rev[f[i]].push_back(s[i]);
  }
  memset(visited, 0, sizeof(visited));
  for (int i = 1; i <= 2 * n; i++) {
    if (occ[i] == 1) continue;
    int sz = dfs(i);
    ans = mul(ans, sz);
  }
  int label = 1;
  for (int i = 1; i <= 2 * n; i++) {
    if (indeg[i] > 0) continue;
    int cur = i;
    bool iscyc = false;
    while (true) {
      visited[cur] = label;
      if (nxt[cur] == cur) {
        iscyc = false;
        break;
      }
      if (nxt[cur] == -1) {
        iscyc = false;
        break;
      }
      if (visited[nxt[cur]] == label) {
        iscyc = true;
        break;
      }
      if (visited[nxt[cur]] != 0) {
        iscyc = false;
        break;
      }
      assert(visited[nxt[cur]] == 0);
      cur = nxt[cur];
    }
    label++;
    if (iscyc) ans = mul(ans, 2);
  }
  for (int i = 1; i <= 2 * n; i++) {
    if (indeg[i] == 0) continue;
    if (visited[i] > 0) continue;
    int cur = i;
    bool iscyc = false;
    while (true) {
      visited[cur] = label;
      if (visited[nxt[cur]] == label) {
        iscyc = true;
        break;
      }
      cur = nxt[cur];
    }
    label++;
    assert(iscyc == true);
    ans = mul(ans, 2);
  }
  cout << ans << endl;
}
