#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int n, first, second, z, V, E;
bool self[200055], vis[200055], bad, asx[200055];
vector<int> e[200055];
long long ans = 1;
void dfs(int first) {
  V++;
  vis[first] = 1;
  for (auto to : e[first]) {
    if (self[to]) bad = 1;
    E++;
    if (!vis[to]) dfs(to);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &first, &second);
    asx[first] = 1;
    if (first == second)
      self[first] = 1;
    else {
      e[first].push_back(second);
      e[second].push_back(first);
    }
  }
  for (int i = 1; i <= n + n; i++)
    if (!self[i] && !vis[i]) {
      V = E = bad = 0;
      dfs(i);
      E /= 2;
      if (bad) continue;
      if (E == V) {
        ans *= 2;
        ans %= MOD;
      } else {
        ans *= V;
        ans %= MOD;
      }
    }
  printf("%lld\n", ans);
  return 0;
}
