#include <bits/stdc++.h>
using namespace std;
const int N = 200005, MOD = 1000000007;
vector<int> rg[N];
int w[N];
bool used[N], vis[N];
int sum(int u) {
  used[u] = true;
  int res = 1;
  for (int v : rg[u]) {
    if (!used[v]) {
      res += sum(v);
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    w[a] = b;
    rg[b].push_back(a);
  }
  long long res = 1;
  for (int i = 1; i <= 2 * n; i++) {
    if (used[i]) {
      continue;
    }
    int t = 0, u = i;
    while (!vis[u]) {
      vis[u] = true;
      if (w[u] == 0) {
        break;
      }
      if (vis[w[u]]) {
        if (w[u] == u) {
          t = 1;
        } else {
          t = 2;
        }
      }
      u = w[u];
    }
    int s = sum(u);
    if (t == 0) {
      res = (res * s) % MOD;
    } else if (t == 2) {
      res = (res * 2) % MOD;
    }
  }
  cout << res << "\n";
}
