#include <bits/stdc++.h>
const int M = 1000000007, N = 200007;
using namespace std;
int prox[N], vis[N];
vector<int> rev[N], vert;
void topo(int u) {
  vis[u] = 1;
  if (prox[u] and vis[prox[u]] == 0) topo(prox[u]);
  vert.push_back(u);
}
long long dfs(int u) {
  vis[u] = 1;
  long long ans = 1;
  for (int i = 0; i < rev[u].size(); ++i) {
    int v = rev[u][i];
    if (vis[v] == 0) ans += dfs(v);
  }
  return ans;
}
int main() {
  int n;
  long long ans;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    prox[u] = v;
    rev[v].push_back(u);
  }
  for (int i = 1; i <= n + n; ++i) {
    if (vis[i] == 0 and prox[i]) topo(i);
  }
  memset(vis, 0, sizeof vis);
  ans = 1;
  for (int i = 0; i < vert.size(); ++i) {
    int u = vert[i];
    if (vis[u] == 0) {
      if (prox[u] == u) {
        dfs(u);
      } else if (prox[u]) {
        dfs(u);
        ans *= 2ll;
      } else
        ans *= (long long)dfs(u);
      ans %= M;
    }
  }
  cout << ans << endl;
}
