#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 * 2 + 7;
const int mod = 1e9 + 7;
int N;
int next_a[maxn];
bool vis[maxn];
int ins[maxn];
vector<int> edge[maxn];
int dfs(int x) {
  int ans = 1;
  vis[x] = 1;
  for (unsigned i = 0; i < edge[x].size(); i++) {
    int y = edge[x][i];
    ans += dfs(y) % mod;
    ans %= mod;
  }
  return ans;
}
int main() {
  scanf("%d", &N);
  for (int i = (1); i <= (2 * N); ++i) edge[i].clear();
  for (int i = (1); i <= (N); ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    next_a[x] = y;
    edge[y].push_back(x);
    ins[y]++;
  }
  long long ans = 1;
  for (int i = (1); i <= (2 * N); ++i)
    if (ins[i] != 0 && next_a[i] == 0 && !vis[i]) {
      int kid_of_way = dfs(i);
      ans *= (long long)(kid_of_way);
      ans %= mod;
    }
  stack<int> sta;
  while (!sta.empty()) sta.pop();
  for (int i = (1); i <= (2 * N); ++i)
    if (!vis[i] && next_a[i] && ins[i] == 0) sta.push(i);
  while (!sta.empty()) {
    int x = sta.top();
    sta.pop();
    vis[x] = 1;
    ins[next_a[x]]--;
    if (ins[next_a[x]] == 0) {
      sta.push(next_a[x]);
    }
  }
  for (int i = (1); i <= (2 * N); ++i)
    if (!vis[i] && next_a[i]) {
      int x = i;
      int tot = 0;
      while (!vis[x]) {
        vis[x] = 1;
        tot++;
        x = next_a[x];
      }
      if (x == i && tot > 1) {
        ans *= 2LL;
        ans %= mod;
      }
    }
  cout << ans << endl;
  return 0;
}
