#include <bits/stdc++.h>
using namespace std;
void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
const int N = 2e5 + 5;
vector<int> adj[N];
bool avail[N], cycle;
int vis[N];
long long ans = 1, cnt;
int dfs(int u, int p) {
  int cnt = 1;
  vis[u] = 2;
  for (auto v : adj[u])
    if (v != p) cnt += dfs(v, u);
  return cnt;
}
void dfs2(int u, int p) {
  vis[u] = 1;
  for (auto v : adj[u]) {
    if (vis[v] == 0) {
      dfs2(v, u);
    } else if (vis[v] == 1 && cycle == false && v != u) {
      cycle = true;
    }
  }
  vis[u] = 2;
}
void solve() {
  int n;
  cin >> n;
  int u, v;
  for (int i = 0; i < n; ++i) {
    cin >> u >> v;
    avail[u] = true;
    adj[v].push_back(u);
  }
  for (int i = 1; i < 2 * n + 1; ++i)
    if (avail[i] == false && adj[i].size() != 0) {
      ans = (ans * 1LL * dfs(i, 0)) % 1000000007;
    }
  for (int i = 1; i < 2 * n + 1; ++i)
    if (avail[i] && vis[i] == 0) {
      cycle = false;
      dfs2(i, 0);
      if (cycle) ans = (ans * 2LL) % 1000000007;
    }
  cout << ans << endl;
  return;
}
int main() {
  boost();
  int tc = 1;
  while (tc--) solve();
  return 0;
}
