#include <bits/stdc++.h>
long long max(long long x, long long y) { return (x > y ? x : y); }
long long min(long long x, long long y) { return (x < y ? x : y); }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
using namespace ::std;
const long long maxn = 2e5 + 500;
const long long inf = 1e9 + 800;
const long long mod = 1e9 + 7;
vector<long long> ger[maxn];
bool vis[maxn];
long long f[maxn];
long long dfs(long long a) {
  vis[a] = 1;
  long long sz = 1;
  for (long long i = 0; i < (long long)ger[a].size(); i++) {
    long long v = ger[a][i];
    if (vis[v] == 0) sz += dfs(v);
  }
  return sz;
}
void dfss(long long a) {
  vis[a] = 1;
  for (long long i = 0; i < (long long)ger[a].size(); i++) {
    long long v = ger[a][i];
    if (vis[v] == 0) dfss(v);
  }
  if (f[a] != -1 && vis[f[a]] == 0) {
    dfss(f[a]);
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  fill(f, f + maxn, -1);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long v, u;
    cin >> v >> u;
    ger[u].push_back(v);
    f[v] = u;
  }
  long long ans = 1;
  for (long long i = 0; i < maxn; i++) {
    if (f[i] == -1) {
      ans = (ans * dfs(i)) % mod;
    }
  }
  for (long long i = 0; i < maxn; i++) {
    if (f[i] == i) {
      dfs(i);
    }
  }
  for (long long i = 0; i < maxn; i++) {
    if (vis[i] == 0) {
      dfss(i);
      ans = (ans * 2) % mod;
    }
  }
  cout << ans;
}
