# @author godmar
from functools import cmp_to_key

def compare_blocks(b1, b2):
    w1, l1 = b1         # assumes   w1 <= l1
    w2, l2 = b2
    if w1 <= w2 and l1 <= l2:
        return -1
    if w1 >= w2 and l1 >= l2:
        return 1
    raise Exception("incomparable")

def arrange(w1, l1, w2, l2):
    a = (l2 - l1 + 1) * (w2 - w1 + 1)
    if l1 <= w2 and l1 != w1:
        a += (l2 - w1 + 1) * (w2 - l1 + 1)
    return a

MOD = 998244353
from collections import Counter
from math import factorial

def solve():
    n = int(input())
    WL = Counter([tuple(sorted(map(int, input().split()))) for i in range(n)])
    wl = list(WL.keys())
    # sort and find out if tower can be built
    try:
        wl.sort(key=cmp_to_key(compare_blocks), reverse=True)
    except Exception as e:
        return 0

    # count arrangements of smaller piece in larger
    lastw, lastl = wl[0]
    count = 1 if lastw == lastl else 2
    for w, l in wl[1:]:
        count = count * arrange(w, l, lastw, lastl) % MOD
        lastw, lastl = w, l

    # account for multiples
    for _wl in wl:
        count = count * factorial(WL[_wl]) % MOD

    return count

T = int(input())
for _ in range(T):
    print (solve())

