#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, m, k, s, t;
int fa[N], size[N], f[N];
long long ans = 1;
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= 2 * n; ++i) {
    fa[i] = i;
    size[i] = 1;
    f[i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    cin >> s >> t;
    if (s == t) {
      s = getfa(s);
      f[s] = 2;
      continue;
    }
    s = getfa(s);
    t = getfa(t);
    if (s == t) {
      f[s] = 1;
      continue;
    }
    fa[s] = t;
    size[t] += size[s];
    if (!f[t]) f[t] = f[s];
  }
  for (int i = 1; i <= 2 * n; ++i)
    if (fa[i] == i) {
      if (f[i] == 1)
        ans = ans * 2 % 1000000007;
      else if (f[i] == 0)
        ans = ans * size[i] % 1000000007;
    }
  cout << ans;
}
