#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e9;
const long double eps = 1e-9;
const long double pi = acos(-1);
const long long int mod = 1000000007;
long long int powmod(long long int a, long long int b) {
  long long int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int parent[200005];
int ranks[200005];
int cycle[200005];
int size[200005];
int n;
void makeset() {
  for (int i = 0; i < 200005; ++i) {
    parent[i] = i;
    ranks[i] = 0;
    size[i] = 1;
  }
}
int dsu_find(int x) {
  return parent[x] = (parent[x] == x ? x : dsu_find(parent[x]));
}
void dsu_union(int p, int q) {
  p = dsu_find(p);
  q = dsu_find(q);
  if (ranks[p] > ranks[q]) parent[q] = p;
  if (ranks[q] > ranks[p]) parent[p] = q;
  if (ranks[p] == ranks[q]) {
    parent[q] = p;
    ranks[p]++;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int i, current, desired;
  makeset();
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> current >> desired;
    current--;
    desired--;
    if (current == desired) {
      current = dsu_find(current);
      cycle[current] = 2;
    } else {
      current = dsu_find(current);
      desired = dsu_find(desired);
      if (dsu_find(current) != dsu_find(desired)) {
        parent[current] = desired;
        size[desired] += size[current];
        cycle[desired] |= cycle[current];
      } else {
        cycle[current] = 1;
      }
    }
  }
  long long int res = 1;
  for (i = 0; i < 2 * n; ++i) {
    if (dsu_find(i) == i) {
      if (cycle[i] == 0)
        res = (res % mod * size[i] % mod) % mod;
      else if (cycle[i] == 1)
        res = (res * 2) % mod;
    }
  }
  cout << res << '\n';
  return 0;
}
