/+ dub.sdl:
    name "F"
    dependency "dcomp" version=">=0.6.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
import std.container.rbtree;
// import dcomp.foundation, dcomp.scanner, dcomp.modint;
// import dcomp.datastructure.unionfind;
// import dcomp.container.deque;

alias Mint = ModInt!(10^^9 + 7);
int[] dec(int[] v) {
    auto w = v.dup.sort!"a<b".uniq.array.assumeSorted;
    return v.map!(x => w.lowerBound(x).length.to!int).array;
}


Mint calc(int[] x, int[] y) {
    int N = x.length.to!int;
    int W = x.fold!"max(a, b)" + 1;
    int H = y.fold!"max(a, b)" + 1;

//    writeln("calc ", x, " ", y);
//    writeln(W, " ", H, " ", N);

    if (W+H != N) return Mint(0);

    int[] dx = (-1).repeat(W).array;
    int[] dy = (-1).repeat(H).array;
    Mint calc2() {

//        writeln("calc2 ", dx, " ", dy);
        if (dx.count(-1)) return Mint(0);
        if (dy.count(-1)) return Mint(0);
        int[][] g = new int[][W+H];
        bool[] leaf = new bool[W+H];
        foreach (i; 0..N) {
            assert((dx[x[i]] == y[i]) ^ (x[i] == dy[y[i]]));
            if (dx[x[i]] == y[i] && dy[y[i]] > x[i]) {
                g[y[i]+W] ~= x[i];
                leaf[x[i]] = true;
            }
            if (x[i] == dy[y[i]] && dx[x[i]] > y[i]) {
                g[x[i]] ~= y[i]+W;
                leaf[y[i]+W] = true;
            }
        }

        int[] sz = new int[N];
        Mint[] D = new Mint[N];
        void dfs(int p) {
            sz[p] = 0;
            D[p] = Mint(1);            
            foreach (int d; g[p]) {
                dfs(d);
                sz[p] += sz[d];
                D[p] *= C(sz[p], sz[d]) * D[d];
            }
            sz[p] += 1;
        }
        int M = N;
        Mint sm = 1;
        foreach (i; 0..N) {
            if (leaf[i]) continue;
            dfs(i);
            sm *= C(M, sz[i]) * D[i];
            M -= sz[i];
        }
        return sm;
    }


    auto cx = iota(W).map!(_ => redBlackTree!int()).array;
    auto cy = iota(H).map!(_ => redBlackTree!int()).array;
    foreach (i; 0..N) {
        cx[x[i]].insert(i);
        cy[y[i]].insert(i);
    }
    Deque!int q;
    foreach (i; 0..W) {
        if (cx[i].length == 0) return Mint(0);
        if (cx[i].length == 1) q.insertBack(i);
    }
    foreach (i; 0..H) {
        if (cy[i].length == 0) return Mint(0);
        if (cy[i].length == 1) q.insertBack(W+i);
    }
    
    bool[] used = new bool[N];
    while (q.empty() == false) {
        int id = q.back; q.removeBack;
        int i;
        if (id < W) {
            //x
            i = cx[id].front;
            if (dx[x[i]] != -1) continue;
            assert(dx[x[i]] == -1);
            dx[x[i]] = y[i];
        } else {
            //y
            id -= W;
            i = cy[id].front;
            if (dy[y[i]] != -1) continue;
            assert(dy[y[i]] == -1);
            dy[y[i]] = x[i];
        }   
        used[i] = true;
        cx[x[i]].removeKey(i);
        if (dx[x[i]] == -1) {
            if (cx[x[i]].length == 0) return Mint(0);
            if (cx[x[i]].length == 1) q.insertBack(x[i]);
        }
        cy[y[i]].removeKey(i);
        if (dy[y[i]] == -1) {
            if (cy[y[i]].length == 0) return Mint(0);
            if (cy[y[i]].length == 1) q.insertBack(W+y[i]);
        }
    }

//    writeln(cx.map!(t => t.length), " ", cy.map!(t => t.length));
//    writeln(dx, " ", dy);

    int[] p;
    void dfs(int i, int d) {
        if (used[i]) return;
        p ~= i;
        used[i] = true;
        if (d == 0) {
            assert(cx[x[i]].length == 2);
            foreach (j; cx[x[i]]) {
                if (i == j) continue;
                dfs(j, 1-d);
            }
        } else {
            assert(cy[y[i]].length == 2);
            foreach (j; cy[y[i]]) {
                if (i == j) continue;
                dfs(j, 1-d);
            }
        }
    }
    foreach (i; 0..N) {
        if (!used[i]) {
            dfs(i, 0);
            break;
        }
    }
//    writeln(p);

    Mint ans = 0;
    foreach (c; 0..2) {
        foreach (idx, i; p) {
            if (idx % 2 == c) {
                dx[x[i]] = y[i];
            } else {
                dy[y[i]] = x[i];
            }
        }
        ans += calc2();
    }
    return ans;
}

Mint solve() {
    int n;
    sc.read(n); n *= 2;
    int[] x = new int[n], y = new int[n];
    auto uf = UnionFind(n);
    int[] xb = (-1).repeat(n/2).array;
    int[] yb = (-1).repeat(n/2).array;
    foreach (i; 0..n) {
        sc.read(x[i], y[i]); x[i]--; y[i]--;
        if (xb[x[i]] != -1) {
            uf.merge(xb[x[i]], i);
        }
        xb[x[i]] = i;
        if (yb[y[i]] != -1) {
            uf.merge(yb[y[i]], i);
        }
        yb[y[i]] = i;
    }

    Mint ans = 1;
    foreach (v; uf.groups) {
        auto m = v.length.to!int;
        if (m == 0) continue;
        int[] nx = v.map!(i => x[i]).array.dec;
        int[] ny = v.map!(i => y[i]).array.dec;
        Mint u = calc(nx, ny);
        ans *= C(n, m) * u;
        n -= m;
//        writeln(v);
//        writeln(nx, " ", ny);
    }
    return ans;
}

Mint[] fact, iFac;
static this() {
    fact = factTable!Mint(1_000_000);
    iFac = invFactTable!Mint(1_000_000);
}
Mint C(int n, int k) {
    if (n < 0 || n < k) return Mint(0);
    return fact[n] * iFac[k] * iFac[n-k];
}

Scanner sc;
static this() {sc = new Scanner(stdin);}
int main() {
    writeln(solve());
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/modint.d */
// module dcomp.modint;

// import dcomp.numeric.primitive;

 
struct ModInt(uint MD) if (MD < int.max) {
    import std.conv : to;
    uint v;
    this(int v) {this(long(v));}
    this(long v) {this.v = (v%MD+MD)%MD;}
    static auto normS(uint x) {return (x<MD)?x:x-MD;}
    static auto make(uint x) {ModInt m; m.v = x; return m;}
     
    auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}
     
    auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}
     
    auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!uint);}
     
    auto opBinary(string op:"/")(ModInt r) const {return this*inv(r);}
    auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}
     
    static ModInt inv(ModInt x) {return ModInt(extGcd!int(x.v, MD)[0]);}
    string toString() {return v.to!string;}
}

 
 

 

 
struct DModInt(string name) {
    import std.conv : to;
    static uint MD;
    uint v;
    this(int v) {this(long(v));}
    this(long v) {this.v = ((v%MD+MD)%MD).to!uint;}
    static auto normS(uint x) {return (x<MD)?x:x-MD;}
    static auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;}
     
    auto opBinary(string op:"+")(DModInt r) const {return make(normS(v+r.v));}
     
    auto opBinary(string op:"-")(DModInt r) const {return make(normS(v+MD-r.v));}
     
    auto opBinary(string op:"*")(DModInt r) const {return make((ulong(v)*r.v%MD).to!uint);}
     
    auto opBinary(string op:"/")(DModInt r) const {return this*inv(r);}
    auto opOpAssign(string op)(DModInt r) {return mixin ("this=this"~op~"r");}
     
    static DModInt inv(DModInt x) {
        return DModInt(extGcd!int(x.v, MD)[0]);
    }
    string toString() {return v.to!string;}
}

 
 

 

template isModInt(T) {
    const isModInt =
        is(T : ModInt!MD, uint MD) || is(S : DModInt!S, string s);
}


T[] factTable(T)(size_t length) if (isModInt!T) {
    import std.range : take, recurrence;
    import std.array : array;
    return T(1).recurrence!((a, n) => a[n-1]*T(n)).take(length).array;
}

 
T[] invFactTable(T)(size_t length) if (isModInt!T) {
    import std.algorithm : map, reduce;
    import std.range : take, recurrence, iota;
    import std.array : array;
    auto res = new T[length];
    res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b";
    foreach_reverse (i, v; res[0..$-1]) {
        res[i] = res[i+1] * T(i+1);
    }
    return res;
}

T[] invTable(T)(size_t length) if (isModInt!T) {
    auto f = factTable!T(length);
    auto invf = invFactTable!T(length);
    auto res = new T[length];
    foreach (i; 1..length) {
        res[i] = invf[i] * f[i-1];
    }
    return res;
}

 
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */
// module dcomp.numeric.primitive;

import std.traits;
import std.bigint;

 
Unqual!T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) {
    return pow(x, n, T(1));
}

 
Unqual!T pow(T, U)(T x, U n, T e) if (isIntegral!U || is(U == BigInt)) {
    Unqual!T b = x, v = e;
    Unqual!U m = n;
    while (m) {
        if (m & 1) v *= b;
        b *= b;
        m /= 2;
    }
    return v;
}

 

 
T lcm(T)(in T a, in T b) {
    import std.numeric : gcd;
    return a / gcd(a,b) * b;
}

 
 

 
 
T[3] extGcd(T)(in T a, in T b) 
if (!isIntegral!T || isSigned!T)  
{
    if (b==0) {
        return [T(1), T(0), a];
    } else {
        auto e = extGcd(b, a%b);
        return [e[1], e[0]-a/b*e[1], e[2]];
    }
}

 
 
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/datastructure/unionfind.d */
// module dcomp.datastructure.unionfind;

 
struct UnionFind {
    import std.algorithm : map, swap, each;
    import std.range : iota, array;
    int[] id;  
    int[][] groups;  
    int count;  
     
    this(int n) {
        id = iota(n).array;
        groups = iota(n).map!(a => [a]).array;
        count = n;
    }
     
    void merge(int a, int b) {
        if (same(a, b)) return;
        count--;
        int x = id[a], y = id[b];
        if (groups[x].length < groups[y].length) swap(x, y);
        groups[y].each!(a => id[a] = x);
        groups[x] ~= groups[y];
        groups[y] = [];
    }
     
    int[] group(int i) {
        return groups[id[i]];
    }
     
    bool same(int a, int b) {
        return id[a] == id[b];
    }
}

 
 

 
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;

 
struct Deque(T, bool mayNull = true) {
    import core.exception : RangeError;
    import core.memory : GC;
    import std.range : ElementType, isInputRange;
    import std.traits : isImplicitlyConvertible;

    struct Payload {
        T *d;
        size_t st, length, cap;
        @property bool empty() const { return length == 0; }
        alias opDollar = length;
        ref inout(T) opIndex(size_t i) inout {
            version(assert) if (length <= i) throw new RangeError();
            return d[(st+i >= cap) ? (st+i-cap) : st+i];
        }
        private void expand() {
            import std.algorithm : max;
            assert(length == cap);
            auto nc = max(size_t(4), 2*cap);
            T* nd = cast(T*)GC.malloc(nc * T.sizeof);
            foreach (i; 0..length) {
                nd[i] = this[i];
            }
            d = nd; st = 0; cap = nc;
        }
        void clear() {
            st = length = 0;
        }
        void insertFront(T v) {
            if (length == cap) expand();
            if (st == 0) st += cap;
            st--; length++;
            this[0] = v; 
        }
        void insertBack(T v) {
            if (length == cap) expand();
            length++;
            this[length-1] = v; 
        }
        void removeFront() {
            assert(!empty, "Deque.removeFront: Deque is empty");        
            st++; length--;
            if (st == cap) st = 0;
        }
        void removeBack() {
            assert(!empty, "Deque.removeBack: Deque is empty");        
            length--;
        }
    }
    struct RangeT(A) {
        alias T = typeof(*(A.p));
        alias E = typeof(A.p.d[0]);
        T *p;
        size_t a, b;
        @property bool empty() const { return b <= a; }
        @property size_t length() const { return b-a; }
        @property RangeT save() { return RangeT(p, a, b); }
        @property RangeT!(const A) save() const {
            return typeof(return)(p, a, b);
        }
        alias opDollar = length;
        @property ref inout(E) front() inout { return (*p)[a]; }
        @property ref inout(E) back() inout { return (*p)[b-1]; }
        void popFront() {
            version(assert) if (empty) throw new RangeError();
            a++;
        }
        void popBack() {
            version(assert) if (empty) throw new RangeError();
            b--;
        }
        ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
        RangeT opSlice() { return this.save; }
        RangeT opSlice(size_t i, size_t j) {
            version(assert) if (i > j || a + j > b) throw new RangeError();
            return typeof(return)(p, a+i, a+j);
        }
        RangeT!(const A) opSlice() const { return this.save; }
        RangeT!(const A) opSlice(size_t i, size_t j) const {
            version(assert) if (i > j || a + j > b) throw new RangeError();
            return typeof(return)(p, a+i, a+j);
        }
    }
    
    alias Range = RangeT!Deque;
    alias ConstRange = RangeT!(const Deque);
    alias ImmutableRange = RangeT!(immutable Deque);
    
    Payload* p;
    private void I() { if (mayNull && !p) p = new Payload(); }
    private void C() const {
        version(assert) if (mayNull && !p) throw new RangeError();
    }
    static if (!mayNull) {
        @disable this();
    }
     
    private this(Payload* p) {
        this.p = p;
    }
    this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {
        p = new Payload();
        foreach (v; values) {
            insertBack(v);
        }
    }
     
    this(Range)(Range r)
    if (isInputRange!Range &&
    isImplicitlyConvertible!(ElementType!Range, T) &&
    !is(Range == T[])) {
        p = new Payload();
        foreach (v; r) {
            insertBack(v);
        }
    }
    static Deque make() { return Deque(new Payload()); }
    @property bool havePayload() const { return (!mayNull || p); }
     
    @property bool empty() const { return (!havePayload || p.empty); }
     
    @property size_t length() const { return (havePayload ? p.length : 0); }
     
    alias opDollar = length;
    ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
     
    ref inout(T) front() inout {C; return (*p)[0]; }
     
    ref inout(T) back() inout {C; return (*p)[$-1]; }
    void clear() { if (p) p.clear(); }
     
    void insertFront(T v) {I; p.insertFront(v); }
     
    void insertBack(T v) {I; p.insertBack(v); }
     
    alias stableInsertBack = insertBack;
     
    void removeFront() {C; p.removeFront(); }
     
    void removeBack() {C; p.removeBack(); }
     
    Range opSlice() {I; return Range(p, 0, length); }
}


 

 

 

 

 

 
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

 
class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray; 
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            if (f.eof) return false;
            line = lineBuf[];
            f.readln(line);
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                 
                 
                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else {
                auto buf = line.split.map!(to!E).array;
                static if (isStaticArray!T) {
                     
                    assert(buf.length == T.length);
                }
                x = buf;
                line.length = 0;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}


 
 

 
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
 
static if (__VERSION__ <= 2070) {
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }
     
}
version (X86) static if (__VERSION__ < 2071) {
    import core.bitop : bsf, bsr, popcnt;
    int bsf(ulong v) {
        foreach (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;
    }
    int bsr(ulong v) {
        foreach_reverse (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;   
    }
    int popcnt(ulong v) {
        int c = 0;
        foreach (i; 0..64) {
            if (v & (1UL << i)) c++;
        }
        return c;
    }
}
