#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const long long int linf = 99999999999999999ll;
const int maxn = 1e6 + 100;
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return (a * b) / gcd(a, b);
}
long long int vis[maxn], edges, ver, f, ans = 1;
vector<int> g[maxn];
void dfs(int s) {
  vis[s] = 1;
  ver++;
  edges += g[s].size();
  for (int i = 0; i < g[s].size(); i++) {
    if (g[s][i] == s) f = 1;
    if (!vis[g[s][i]]) dfs(g[s][i]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, u, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> v;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  for (int i = 1; i <= 2 * n; i++) {
    if (!vis[i]) {
      edges = 0, ver = 0, f = 0;
      dfs(i);
      if (f) continue;
      edges /= 2;
      if (ver == edges)
        ans = (ans * 2) % 1000000007;
      else
        ans = (ans * ver) % 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}
