#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;
int n, res = 1, p[N], sz[N], b[N];
bool mark[N];
int Find(int u) { return u == p[u] ? u : p[u] = Find(p[u]); }
void join(int u, int v) {
  sz[v] += sz[u];
  b[v] *= b[u];
  p[u] = v;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * 2; ++i) p[i] = i, sz[i] = b[i] = 1;
  for (int i = 0, u, v; i < n; ++i) {
    scanf("%d%d", &u, &v);
    if (u == v) {
      b[Find(u)] = -1;
      continue;
    }
    u = Find(u);
    v = Find(v);
    if (u != v)
      join(u, v);
    else
      b[u] = 2;
  }
  for (int i = 1; i <= n * 2; ++i) {
    int k = Find(i);
    if (mark[k]) continue;
    mark[k] = 1;
    if (b[k] == -1)
      ;
    else if (b[k] == 2)
      res = 2LL * res % mod;
    else
      res = 1LL * res * sz[k] % mod;
  }
  printf("%d\n", res);
}
