Problem:
Score : 1200 points

Problem Statement

There are 2N balls in the xy-plane. The coordinates of the i-th of them is (x_i, y_i).
Here, x_i and y_i are integers between 1 and N (inclusive) for all i, and no two balls occupy the same coordinates.

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the type-A robots at coordinates (1, 0), (2, 0), ..., (N, 0), and the type-B robots at coordinates (0, 1), (0, 2), ..., (0, N), one at each position.

When activated, each type of robot will operate as follows.

When a type-A robot is activated at coordinates (a, 0), it will move to the position of the ball with the lowest y-coordinate among the balls on the line x = a, collect the ball and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

When a type-B robot is activated at coordinates (0, b), it will move to the position of the ball with the lowest x-coordinate among the balls on the line y = b, collect the ball and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

Once deactivated, a robot cannot be activated again. Also, while a robot is operating, no new robot can be activated until the operating robot is deactivated.

When Snuke was about to activate a robot, he noticed that he may fail to collect all the balls, depending on the order of activating the robots.

Among the (2N)! possible orders of activating the robots, find the number of the ones such that all the balls can be collected, modulo 1 000 000 007.

Constraints

2 \leq N \leq 10^5

1 \leq x_i \leq N

1 \leq y_i \leq N

If i ≠ j, either x_i ≠ x_j or y_i ≠ y_j.

Inputs

Input is given from Standard Input in the following format:

N
x_1 y_1
...
x_{2N} y_{2N}

Outputs

Print the number of the orders of activating the robots such that all the balls can be collected, modulo 1 000 000 007.

Sample Input 1

2
1 1
1 2
2 1
2 2

Sample Output 1

8

We will refer to the robots placed at (1, 0) and (2, 0) as A1 and A2, respectively, and the robots placed at (0, 1) and (0, 2) as B1 and B2, respectively.
There are eight orders of activation that satisfy the condition, as follows:

A1, B1, A2, B2

A1, B1, B2, A2

A1, B2, B1, A2

A2, B1, A1, B2

B1, A1, B2, A2

B1, A1, A2, B2

B1, A2, A1, B2

B2, A1, B1, A2

Thus, the output should be 8.

Sample Input 2

4
3 2
1 2
4 1
4 2
2 2
4 4
2 1
1 3

Sample Output 2

7392

Sample Input 3

4
1 1
2 2
3 3
4 4
1 2
2 1
3 4
4 3

Sample Output 3

4480

Sample Input 4

8
6 2
5 1
6 8
7 8
6 5
5 7
4 3
1 4
7 6
8 3
2 8
3 6
3 2
8 5
1 5
5 8

Sample Output 4

82060779

Sample Input 5

3
1 1
1 2
1 3
2 1
2 2
2 3

Sample Output 5

0

When there is no order of activation that satisfies the condition, the output should be 0.

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

use std::io::*;
use std::str::FromStr;
use std::collections::HashMap;
use std::collections::HashSet;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
fn pow_mod(base: i64, power: i64, modulo: i64) -> i64 {
    let mut base = base;
    let mut power = power;
    let mut ans = 1;
    while power > 0 {
        if power & 1 == 1 {
            ans = ans * base % modulo;
        }
        power >>= 1;
        base = base * base % modulo;
    }
    ans
}

#[allow(dead_code)]
fn inv_mod(a: i64, modulo: i64) -> i64 {
    pow_mod(a, modulo - 2, modulo)
}

const MOD: i64 = 1e+9 as i64 + 7;
const FACT_MAX: usize = 100100;
static mut FACTORIAL: [i64; FACT_MAX + 1] = [0; FACT_MAX + 1];
static mut RFACTORIAL: [i64; FACT_MAX + 1] = [0; FACT_MAX + 1];
fn init_fact() {
    unsafe {
        if FACTORIAL[0] == 0 {
            FACTORIAL[0] = 1;
            for i in 1..FACT_MAX + 1 {
                FACTORIAL[i] = (FACTORIAL[i - 1] * i as i64) % MOD;
            }
            RFACTORIAL[FACT_MAX] = inv_mod(FACTORIAL[FACT_MAX], MOD);
            for i in (0..FACT_MAX).rev() {
                RFACTORIAL[i] = RFACTORIAL[i + 1] * (i as i64 + 1) % MOD;
            }
        }
    }
}
#[allow(dead_code)]
fn fact(n: usize) -> i64 {
    init_fact();
    unsafe { FACTORIAL[n] }
}
#[allow(dead_code, unused_comparisons)]
fn combi(n: usize, m: usize) -> i64 {
    if n < m || m < 0 {
        return 0;
    }
    init_fact();
    unsafe { FACTORIAL[n] * RFACTORIAL[m] % MOD * RFACTORIAL[n - m] % MOD }
}

#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
struct Edge {
    index: usize,
    src: usize,
    dest: usize,
}
#[allow(dead_code)]
impl Edge {
    fn new(index: usize, src: usize, dest: usize) -> Edge {
        Edge {
            index: index,
            src: src,
            dest: dest,
        }
    }
}
impl std::cmp::Ord for Edge {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (self.src, self.dest).cmp(&(other.src, other.dest))
    }
}
impl std::cmp::PartialOrd for Edge {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
type Edges = Vec<Edge>;
type Graph = Vec<Edges>;
#[allow(dead_code)]
fn graph_new(n: usize) -> Graph {
    vec![vec![]; n]
}
#[allow(dead_code)]
fn add_bi_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize) {
    graph[src].push(Edge::new(*index, src, dest));
    graph[dest].push(Edge::new(*index, dest, src));
    *index += 1;
}

fn merge(lhs: (usize, usize), rhs: (usize, usize)) -> (usize, usize) {
    let cnt = lhs.1 + rhs.1;
    let v = lhs.0 as i64 * rhs.0 as i64 % MOD * combi(cnt, lhs.1) % MOD;
    (v as usize, cnt)
}

struct Solver {
    n: usize,
    g: Graph,
}

impl Solver {
    fn new() -> Solver {
        let n: usize = read();
        let mut g = graph_new(2 * n);
        let mut e = 0;
        for _ in 0..n * 2 {
            let x = read::<usize>() - 1;
            let y = read::<usize>() - 1;
            add_bi_edge(&mut g, &mut e, y, x + n);
        }
        Solver { n: n, g: g }
    }
    fn usable(&self, constraint: &Edge, edge: &Edge) -> bool {
        edge.dest < constraint.dest
    }
    fn dfs1(&mut self, from: usize, connected: &mut HashSet<usize>) -> usize {
        if connected.contains(&from) {
            return 0;
        }
        let mut ret = self.g[from].len();
        connected.insert(from);
        for edge in self.g[from].clone().iter() {
            ret += self.dfs1(edge.dest, connected);
        }
        ret
    }
    fn set_direction(&self, connected: &HashSet<usize>, dir: usize) -> HashMap<usize, Edge> {
        let mut edge_cnt = HashMap::<usize, usize>::new();
        let mut stack = vec![];
        for &node in connected.iter() {
            let cnt = self.g[node].len();
            edge_cnt.insert(node, cnt);
            if cnt == 1 {
                stack.push(node);
            }
        }
        let mut ret = HashMap::<usize, Edge>::new();
        let mut used_edge = HashSet::<usize>::new();
        let mut second_start = false;
        for _ in 0..2 {
            while let Some(from) = stack.pop() {
                for edge in self.g[from].clone().iter() {
                    // println!("{:?}", edge);
                    if used_edge.contains(&edge.index) {
                        continue;
                    }
                    if second_start {
                        second_start = false;
                        continue;
                    }
                    used_edge.insert(edge.index);
                    assert!(!ret.contains_key(&from));
                    ret.insert(from, edge.clone());
                    *edge_cnt.get_mut(&edge.dest).unwrap() -= 1;
                    if edge_cnt[&edge.dest] == 1 {
                        stack.push(edge.dest);
                    }
                    break;
                }
            }
            if dir == 1 {
                second_start = true;
            }
            // println!("{:?}", ret);
            for &node in connected.iter() {
                if edge_cnt[&node] == 2 {
                    stack.push(node);
                    break;
                }
            }
        }
        ret
    }
    fn calc(&self, from: usize, matching: &HashMap<usize, Edge>) -> (usize, usize) {
        let mut ret = (1, 0);
        for edge in self.g[from].iter() {
            if !self.usable(&matching[&from], edge) {
                continue;
            }
            // println!("{:?} {:?}", matching[&from], edge);
            let nret = self.calc(edge.dest, matching);
            ret = merge(ret, nret);
        }
        ret.1 += 1;
        ret
    }
    fn solve(&mut self) {
        let mut ans = (1, 0);
        let mut visited = vec![false; 2 * self.n];
        for node in 0..2 * self.n {
            if visited[node] {
                continue;
            }
            let mut connected = HashSet::<usize>::new();
            let edge_cnt = self.dfs1(node, &mut connected) / 2;
            if edge_cnt != connected.len() {
                ans = (0, 0);
                break;
            }
            for &node in connected.iter() {
                visited[node] = true;
            }
            let mut sum = 0;
            for dir in 0..2 {
                // println!("calc connect: {} {}", node, dir);
                let matching = self.set_direction(&connected, dir);
                // assert!(matching.len() == connected.len());
                // let mut lans = (1, 0);
                // // println!("{:?}", matching);
                // for &node in connected.iter() {
                //     let dest = matching[&node].dest;
                //     if !self.usable(&matching[&dest], &Edge::new(0, dest, node)) {
                //         let mut nret = self.calc(node, &matching);
                //         // println!("{:?}", nret);
                //         lans = merge(lans, nret);
                //     }
                // }
                // sum = (sum + lans.0) % MOD as usize;
            }
            ans = merge(ans, (sum, connected.len()));
        }
        println!("{}", ans.0);
    }
}

fn main() {
    let mut solver = Solver::new();
    solver.solve();
}