#include <bits/stdc++.h>
using namespace std;
const int N = 200002;
int n;
vector<int> adjL[N];
int numOfNodes = 0;
bool vis[N], notEmpty[N], hasCycle, isTer;
void dfs(int node, int p) {
  if (!notEmpty[node]) isTer = true;
  if (vis[node]) {
    hasCycle = true;
    return;
  }
  ++numOfNodes;
  vis[node] = true;
  bool done = false;
  for (int ch : adjL[node]) {
    if (ch == p && !done) {
      done = true;
      continue;
    }
    dfs(ch, node);
  }
}
int main() {
  scanf("%d", &n);
  for (int(i) = (1); (i) <= (int)(n); (i)++) {
    int a, b;
    scanf("%d%d", &a, &b);
    notEmpty[a] = true;
    if (b != a) adjL[b].push_back(a), adjL[a].push_back(b);
  }
  long long ans = 1;
  for (int(i) = (1); (i) <= (int)(2 * n); (i)++)
    if (!vis[i] && !adjL[i].empty()) {
      hasCycle = false, isTer = false, numOfNodes = 0;
      dfs(i, -1);
      if (hasCycle)
        ans = ans * 2 % 1000000007;
      else if (isTer)
        ans = ans * numOfNodes % 1000000007;
    }
  printf("%lld\n", ans);
  return 0;
}
