#include <bits/stdc++.h>
using namespace std;
long long n;
map<long long, vector<long long>> adj;
map<long long, bool> hv;
map<long long, bool> oc;
long long cntr = 0;
bool lp = false;
int ed = 0;
void find(long long curNode, long long prevNode) {
  cntr++;
  hv[curNode] = true;
  ed += adj[curNode].size();
  for (long long i : adj[curNode]) {
    if (i == curNode) lp = true;
    if (hv[i]) continue;
    find(i, curNode);
  }
}
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long u, v;
    cin >> u >> v;
    oc[u] = true;
    oc[v] = true;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  long long ans = 1;
  for (pair<long long, bool> p : oc) {
    long long i = p.first;
    if (hv[i]) continue;
    cntr = lp = ed = 0;
    find(i, -1);
    ed /= 2;
    if (lp) {
      continue;
    }
    if (ed == cntr) {
      ans *= 2;
      ans %= 1000000007;
      continue;
    }
    ans *= cntr;
    ans %= 1000000007;
  }
  cout << ans << endl;
}
