#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long double EPS = 1e-8;
const long double PI = acos(-1.0L);
const int MAXN = 2e5 + 100;
long long MOD = 1e9 + 7;
int p[MAXN];
bool taken[MAXN];
int used[MAXN];
int timer = 1;
vector<int> c[MAXN];
long long getans(int v) {
  long long ans = 1;
  for (auto u : c[v]) {
    ans += getans(u);
  }
  return ans % MOD;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = int(0); i < int(n); ++i) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    taken[u] = true;
    p[u] = v;
    c[v].push_back(u);
  }
  long long ans = 1;
  for (int i = int(0); i < int(2 * n); ++i) {
    if (!taken[i]) continue;
    timer++;
    int v = i;
    bool tree = true;
    while (true) {
      if (used[v] != 0) {
        tree = false;
        if (used[v] == timer) {
          if (p[v] != v) {
            ans *= 2;
            ans %= MOD;
          }
        }
        break;
      }
      used[v] = timer;
      if (!taken[v]) {
        break;
      }
      v = p[v];
    }
    if (tree) {
      ans *= getans(v);
      ans %= MOD;
    }
  }
  cout << ans << '\n';
  return 0;
}
