#include <bits/stdc++.h>
namespace IO {
const unsigned int Buffsize = 1 << 15, Output = 1 << 23;
static char Ch_[Buffsize], *S_ = Ch_, *T_ = Ch_;
static char Out[Output], *nowps = Out;
inline void flush() {
  fwrite(Out, 1, nowps - Out, stdout);
  nowps = Out;
}
template <typename T>
inline T Read() {
  T _ = 0;
  static char __;
  T ___ = 1;
  for (__ = ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin),
                           S_ == T_)
                 ? 0
                 : *S_++);
       !isdigit(__);
       __ = ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin),
                           S_ == T_)
                 ? 0
                 : *S_++))
    if (__ == '-') ___ = -1;
  for (; isdigit(__);
       __ = ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin),
                           S_ == T_)
                 ? 0
                 : *S_++))
    _ = (_ << 3) + (_ << 1) + (__ ^ 48);
  return _ *= ___;
}
template <typename T>
inline void write(T _, char __ = '\n') {
  if (!_) *nowps++ = '0';
  if (_ < 0) *nowps++ = '-', _ = -_;
  static unsigned int sta[111], tp;
  for (tp = 0; _; _ /= 10) sta[++tp] = _ % 10;
  for (; tp; *nowps++ = sta[tp--] ^ 48)
    ;
  (*nowps++) = __;
}
template <class T>
inline bool chkmax(T &_, T __) {
  return _ < __ ? _ = __, 1 : 0;
}
template <class T>
inline bool chkmin(T &_, T __) {
  return _ > __ ? _ = __, 1 : 0;
}
}  // namespace IO
using namespace std;
using namespace IO;
const int N = 2e5 + 10, Mod = 1e9 + 7;
int n;
int fa[N], sz[N], flag[N], ans;
int find(int first) {
  return first == fa[first] ? first : fa[first] = find(fa[first]);
}
void merge(int first, int second) {
  int fx = find(first), fy = find(second);
  if (fx != fy) {
    fa[fx] = fy;
    sz[fy] += sz[fx];
    flag[fy] |= flag[fx];
  } else
    flag[fy] = 1;
}
int main() {
  n = Read<int>();
  for (int i = 1; i <= 2 * n; ++i) fa[i] = i, sz[i] = 1, flag[i] = 0;
  for (register int i = (1), i_end_ = (n); i <= i_end_; ++i) {
    int u = Read<int>(), v = Read<int>();
    if (u != v)
      merge(u, v);
    else
      flag[u] = 2;
  }
  ans = 1;
  for (register int i = (1), i_end_ = (2 * n); i <= i_end_; ++i)
    if (find(i) == i) {
      if (flag[i] == 0)
        ans = (1ll * ans * sz[i]) % Mod;
      else if (flag[i] == 1)
        ans = (ans * 2ll) % Mod;
    }
  printf("%d\n", ans);
  return flush(), 0;
}
