#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
const int maxn = 210000;
const long long P = 1000000000 + 7;
int to[maxn];
vector<int> e[maxn];
int vis[maxn];
bool dfs(int v) {
  if (vis[v] == 1) return true;
  vis[v] = 1;
  for (int u : e[v])
    if (dfs(u)) return true;
  vis[v] = 2;
  return false;
}
int dfsw(int v) {
  int res = 1;
  vis[v] = 1;
  for (int u : e[v]) res += dfsw(u);
  vis[v] = 2;
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  int n;
  cin >> n;
  for (int i = 0; i < (int)(2 * n); ++i) to[i] = -1;
  for (int i = 0; i < (int)(n); ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    to[x] = y;
    e[y].push_back(x);
  }
  long long ans = 1;
  for (int i = 0; i < (int)(2 * n); ++i) {
    if (!vis[i] && dfs(i) && to[i] != i) (ans *= 2) %= P;
  }
  for (int i = 0; i < (int)(2 * n); ++i)
    if (vis[i] != 1) vis[i] = 0;
  for (int i = 0; i < (int)(2 * n); ++i) {
    if (vis[i] || to[i] != -1) continue;
    (ans *= dfsw(i)) %= P;
  }
  cout << ans << '\n';
  return 0;
}
