#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int mod = 1e9 + 7;
int n;
bool vis[N];
vector<int> g[N];
void dfs(int u, int &nn, int &mm, int &cycle) {
  vis[u] = 1;
  nn++;
  for (int i = 0; i < ((int)g[u].size()); i++) {
    int v = g[u][i];
    mm++;
    if (v == u) {
      cycle++;
      continue;
    }
    if (!vis[v]) {
      dfs(v, nn, mm, cycle);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0, u, v; i < n; i++) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    if (u != v) g[v].push_back(u);
  }
  n <<= 1;
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      int nn = 0, mm = 0, cycle = 0;
      dfs(i, nn, mm, cycle);
      mm = (mm - cycle) / 2 + cycle;
      if (mm > nn)
        ans = 0;
      else if (mm == nn) {
        if (!cycle) ans = 1LL * ans * 2 % mod;
      } else {
        assert(mm == nn - 1);
        ans = 1LL * ans * nn % mod;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
