You are given a sequence of $n$ pairs of integers: $(a_1, b_1), (a_2, b_2), \dots , (a_n, b_n)$. This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences:  $s = [(1, 2), (3, 2), (3, 1)]$ is bad because the sequence of first elements is sorted: $[1, 3, 3]$;  $s = [(1, 2), (3, 2), (1, 2)]$ is bad because the sequence of second elements is sorted: $[2, 2, 2]$;  $s = [(1, 1), (2, 2), (3, 3)]$ is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted;  $s = [(1, 3), (3, 3), (2, 2)]$ is good because neither the sequence of first elements $([1, 3, 2])$ nor the sequence of second elements $([3, 3, 2])$ is sorted. 

Calculate the number of permutations of size $n$ such that after applying this permutation to the sequence $s$ it turns into a good sequence. 

A permutation $p$ of size $n$ is a sequence $p_1, p_2, \dots , p_n$ consisting of $n$ distinct integers from $1$ to $n$ ($1 \le p_i \le n$). If you apply permutation $p_1, p_2, \dots , p_n$ to the sequence $s_1, s_2, \dots , s_n$ you get the sequence $s_{p_1}, s_{p_2}, \dots , s_{p_n}$. For example, if $s = [(1, 2), (1, 3), (2, 3)]$ and $p = [2, 3, 1]$ then $s$ turns into $[(1, 3), (2, 3), (1, 2)]$.


-----Input-----

The first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$).

The next $n$ lines contains description of sequence $s$. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le n$) — the first and second elements of $i$-th pair in the sequence.

The sequence $s$ may contain equal elements.


-----Output-----

Print the number of permutations of size $n$ such that after applying this permutation to the sequence $s$ it turns into a good sequence. Print the answer modulo $998244353$ (a prime number).


-----Examples-----
Input
3
1 1
2 2
3 1

Output
3

Input
4
2 3
2 2
2 1
2 4

Output
0

Input
3
1 1
1 1
2 3

Output
4



-----Note-----

In first test case there are six permutations of size $3$:   if $p = [1, 2, 3]$, then $s = [(1, 1), (2, 2), (3, 1)]$ — bad sequence (sorted by first elements);  if $p = [1, 3, 2]$, then $s = [(1, 1), (3, 1), (2, 2)]$ — bad sequence (sorted by second elements);  if $p = [2, 1, 3]$, then $s = [(2, 2), (1, 1), (3, 1)]$ — good sequence;  if $p = [2, 3, 1]$, then $s = [(2, 2), (3, 1), (1, 1)]$ — good sequence;  if $p = [3, 1, 2]$, then $s = [(3, 1), (1, 1), (2, 2)]$ — bad sequence (sorted by second elements);  if $p = [3, 2, 1]$, then $s = [(3, 1), (2, 2), (1, 1)]$ — good sequence.

Difficulty: interview

Starter code:

Solutions:
import sys
from collections import Counter
def input():
	return sys.stdin.readline()[:-1]

MOD = 998244353
n = int(input())
fact = [1]
for i in range(1, n+1):
	fact.append((fact[-1]*i)%MOD)

seq = []
ca, cb = Counter(), Counter()
for _ in range(n):
	a, b = map(int, input().split())
	ca[a] += 1
	cb[b] += 1
	seq.append((a, b))

ans = fact[n]
ans %= MOD
#print(ans)

res = 1
for v in ca.values():
	res *= fact[v]
	res %= MOD
ans -= res
ans %= MOD
#print(ans)

res = 1
for v in cb.values():
	res *= fact[v]
	res %= MOD
ans -= res
#print(ans)

seq.sort(key=lambda x: (x[0], x[1]))
cur = seq[0][0]
res = 1
M = 1
ctmp = Counter()
for i in range(n):
	if seq[i][0] == cur:
		ctmp[seq[i][1]] += 1
		M = max(M, seq[i][1])
	else:
		if seq[i][1] < M:
			res = 0
			break
		tmp = 1
		for v in ctmp.values():
			tmp *= fact[v]
			tmp %= MOD
		res *= tmp
		res %= MOD
		ctmp = Counter()
		ctmp[seq[i][1]] += 1
		cur = seq[i][0]
		M = max(M, seq[i][1])
tmp = 1
for v in ctmp.values():
	tmp *= fact[v]
	tmp %= MOD
res *= tmp
res %= MOD

ans += res
ans %= MOD
print(ans)

import sys
input = sys.stdin.readline

mod=998244353

n=int(input())
S=[list(map(int,input().split())) for i in range(n)]

FACT=[1]
for i in range(1,3*10**5+10):
    FACT.append(FACT[-1]*i%mod)

X=[x for x,y in S]
Y=[y for x,y in S]

ANS=1
for i in range(1,n+1):
    ANS=ANS*i%mod

X.sort()
XA=1
count=1
for i in range(1,n):
    if X[i]==X[i-1]:
        count+=1
    else:
        XA=XA*FACT[count]%mod
        count=1
        
XA=XA*FACT[count]%mod

Y.sort()
YA=1
count=1
for i in range(1,n):
    if Y[i]==Y[i-1]:
        count+=1
    else:
        YA=YA*FACT[count]%mod
        count=1
YA=YA*FACT[count]%mod


S.sort()
SA=1
count=1
for i in range(1,n):
    if S[i][1]<S[i-1][1]:
        SA=0
        break
    
    if S[i]==S[i-1]:
        count+=1
    else:
        SA=SA*FACT[count]%mod
        count=1

SA=SA*FACT[count]%mod

    

print((ANS-XA-YA+SA)%mod)


import sys

p = 998244353
n = int(sys.stdin.readline())
S1 = []
S2 = []
A = []
for i in range (0, n):
    x, y = list(map(int, sys.stdin.readline().strip().split()))
    S1.append(x)
    S2.append(y)
    A.append(x * 1000000 + y)
S1.sort()
S2.sort()
A.sort()
x = S1[0]
c = 1
ans1 = 1
for i in range (1, n):
    if S1[i] == x:
        c = c + 1
        ans1 = (ans1 * c) % p
    else:
        x = S1[i]
        c = 1
c = 1
ans2 = 1
x = S2[0]
for i in range (1, n):
    if S2[i] == x:
        c = c + 1
        ans2 = (ans2 * c) % p
    else:
        x = S2[i]
        c = 1
c = 1
ans3 = 1
x = A[0]
for i in range (1, n):
    if A[i] == x:
        c = c + 1
        ans3 = (ans3 * c) % p
    else:
        x = A[i]
        c = 1
    if A[i] % 1000000 < A[i-1] % 1000000:
        ans3 = 0
ans4 = 1
for i in range (1, n + 1):
    ans4 = ans4 * i % p
print((ans4 - ans1 -ans2 + ans3) % p)


    



from sys import stdin
input = stdin.readline
n = int(input())
p = 998244353
sil = [0] * 400000
sil[1] = 1
for i in range(2, 400000):
    sil[i] = (sil[i -1] * i) % p
def fun(l):
    wyn = 1
    l.sort()
    count = 1
    for i in range(1, len(l)):
        if l[i] != l[i-1]:
            wyn = (wyn * sil[count]) % p
            count = 1
        else:
            count += 1
    wyn = (wyn * sil[count]) % p
    return wyn
dupa = [list(map(int,input().split())) for i in range(n)]
xx = [i[0] for i in dupa]
yy = [i[1] for i in dupa]
dupa.sort()
x = [i[0] for i in dupa]
y = [i[1] for i in dupa]
dolne = []
pyk = 1
left = 0
for i in range(1, n):
    if x[i] != x[i-1]:
        dolne.append(y[left:i])
        left = i
if left == n - 1:
    dolne.append([y[n - 1]])
else:
    dolne.append(y[left:n])
c = 1
for i in range(1, len(dolne)):
    if min(dolne[i]) < max(dolne[i - 1]):
        c = 0
        break
# c = 0 means no overlaps

wyn = fun(xx) + fun(yy)
if c == 1:
    dupa = 1
    for i in range(len(dolne)):
        dupa = (dupa * fun(dolne[i])) % p
    wyn -= dupa
print((sil[n] - wyn) % p)

from collections import Counter
import sys
input = sys.stdin.readline
MOD = 998244353 
INF = 10**10

n = int(input())
s = []
a_s = []
b_s = []
for i in range(n):
    a, b = [int(item) for item in input().split()]
    s.append((a, b))
    a_s.append(a)
    b_s.append(b)
s.sort()
s.append((INF, INF))

# Calc facs
facs = [1] * (n + 1)
ans = 1
for i in range(1, n+1):
    facs[i] = facs[i-1] * i % MOD
ans = facs[n]

# Check both sort
pa = 0; pb = 0
both_cnt = 1
same = 1
for a, b in s:
    if a == pa and b == pb:
        same += 1
    else:
        if b < pb:
            both_cnt = 0
            break
        both_cnt *= facs[same]
        both_cnt %= MOD
        same = 1
    pa = a; pb = b

# Check upper sort
acnt = Counter(a_s)
first_cnt = 1
for key in acnt.keys():
    first_cnt *= facs[acnt[key]]
    first_cnt %= MOD

# Check lower sort
bcnt = Counter(b_s)
second_cnt = 1
for key in bcnt.keys():
    second_cnt *= facs[bcnt[key]]
    second_cnt %= MOD

print((ans - (first_cnt + second_cnt - both_cnt) + MOD) % MOD)

# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
from sys import stdin, stdout
from collections import defaultdict
import math
import copy
 
#T = int(input())
N = int(input())
#s = input()
#N,M = [int(x) for x in stdin.readline().split()]
#arr = [int(x) for x in stdin.readline().split()]
 
permu = [0]*N
permu[0] = 1
for i in range(1,N):
    permu[i] = (permu[i-1]*(i+1))%998244353

data = []
pair = {}
for i in range(N):
    a,b = [int(x) for x in stdin.readline().split()]
    data.append((a,b))
    if a not in pair:
        pair[a] = []
        
    pair[a].append(b)
    
    
freq_one = {}
for i in range(N):
    a = data[i][0]
    if a not in freq_one:
        freq_one[a] = 1
    else:
        freq_one[a] += 1
        
all = permu[-1]

# first column
p1 = 1
for key in freq_one:
    f = freq_one[key]
    p1 *= permu[f-1]
    p1 = p1%998244353

freq_two = {}
for i in range(N):
    a = data[i][1]
    if a not in freq_two:
        freq_two[a] = 1
    else:
        freq_two[a] += 1
        
# second column        
p2 = 1
for key in freq_two:
    f = freq_two[key]
    p2 *= permu[f-1]
    p2 = p2%998244353
    
# both sorted
max_second = 0
P = list(pair.keys())
P.sort()

p3 = 1
for key in P:
    #print(key)
    mini = min(pair[key])
    if mini<max_second:
        p3 = 0
        break
    
    f = {}
    for a in pair[key]:
        if a not in f:
            f[a] = 1
        else:
            f[a] += 1
            
    p = 1
    for num in f:
        fr = f[num]
        p *= permu[fr-1]
        p = p%998244353
        
    p3 *= p
    p3 = p3%998244353
    
    max_second = max(pair[key])
    
ans = (all-p1-p2+p3)%998244353

print(ans)


    



import sys

in_file = sys.stdin#

n = int(in_file.readline().strip())
Al = []
Ar = []
A = []
for _ in range(n):
    x, y = map(int, in_file.readline().strip().split())
    Al.append(x)
    Ar.append(y)
    A.append((x,y))

Al.sort()
Ar.sort()
A.sort()

def groups(L):
    p = L[0]
    G = [1]
    for i in range(1, len(L)):
        if p == L[i]:
            G[-1] += 1
        else:
            p = L[i]
            G.append(1)
    return G

def bin_exp(x, a, p):
    r = 1
    while a>0:
        if a%2 == 1:
            r = (r*x)%p
        x = (x*x)%p
        a = a//2
    return r

def mod_fact(x, p):
    r = 1
    for i in range(1, x+1):
        r = (r*i)%p
    return r

p = 998244353
Gl = groups(Al)
rl = 1
for i in Gl:
    rl = (rl*mod_fact(i, p))%p
Gr = groups(Ar)
rr = 1
for i in Gr:
    rr = (rr*mod_fact(i, p))%p
G = groups(A)
r = 1
for i in G:
    r = (r*mod_fact(i, p))%p
if list(zip(Al, Ar)) != A:
    r = 0
res = (mod_fact(len(A), p)+r)%p
res = (p+res-rl)%p
res = (p+res-rr)%p

sys.stdout.write(str(res))
sys.stdout.flush() 

'''input
3
1 1
2 2
3 1

'''
import sys
from collections import defaultdict as dd
mod=998244353  

def ri(flag=0):
	if flag==0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())


n = ri(1)

one = []
sec = []
take = []
for i in range(n):
	a, b = ri()
	one.append(a)
	sec.append(b)
	take.append((a,b))

ans = 1
temp =1

for i in range(1,n+1):
	ans = (ans*i)%mod

take.sort()

# good good
cnt1=0
cnt2=0
# check = [[0,0] for i in range(n)]

# for i in range(1,n):
# 	if take[i][0] != take[i-1][0]:
# 		check[i][0] = check[i-1][0]+1
# 	else:
# 		check[i][0] = check[i-1][0]

# 	if take[i][1] != take[i-1][1]:
# 		check[i][1] = check[i-1][1]+1
# 	else:
# 		check[i][1] = check[i-1][1]

c1= dd(int)
c2 = dd(int)
c3 =dd(int)


status=1

for i in range(1,n):
	if take[i][1]<take[i-1][1]:
		status=0

for i in one:
	c1[i]+=1
for i in sec:
	c2[i]+=1
for i in take:
	c3[i]+=1

ok1=1
ok2=1
ok3=1

#print(c1,c2,c3)

fac= [1 for i in range(3*10**5+1)]

for i in range(1,3*10**5+1):
	fac[i] = (fac[i-1]*i)%mod

for i in c1:
	ok1 = (ok1*fac[c1[i]])%mod

for i in c2:
	ok2 = (ok2*fac[c2[i]])%mod

for i in c3:
	ok3 = (ok3*fac[c3[i]])%mod

if status==0:
	ok3 = 0

print( (ans - ok1 - ok2 +ok3)%mod)


from operator import itemgetter
import sys


class Combination:
    '''MOD上の
    計算量：階乗・逆元テーブルの作成O(N)
    nCkを求めるO(1)'''

    def __init__(self, n, MOD):
        self.fact = [1]
        for i in range(1, n + 1):
            self.fact.append(self.fact[-1] * i % MOD)


input = sys.stdin.readline
n = int(input())
p = [list(map(int, input().split())) for i in range(n)]
MOD = 998244353
comb = Combination(n, MOD)
memo1 = {}
memo2 = {}

for i in range(n):
    tmp1, tmp2 = p[i]
    if tmp1 not in memo1:
        memo1[tmp1] = 1
    else:
        memo1[tmp1] += 1
    if tmp2 not in memo2:
        memo2[tmp2] = 1
    else:
        memo2[tmp2] += 1

ans = 0

tmp = 1
for i in memo1:
    tmp *= comb.fact[memo1[i]]
    tmp %= MOD
ans += tmp
ans %= MOD

tmp = 1
for i in memo2:
    tmp *= comb.fact[memo2[i]]
    tmp %= MOD
ans += tmp
ans %= MOD

p = sorted(p, key = itemgetter(1))
p = sorted(p, key = itemgetter(0))
for i in range(n-1):
    if p[i][1] > p[i+1][1]:
        ans = comb.fact[n] - ans
        ans %= MOD
        print(ans)
        return
cnt = 1
tmp = 1
for i in range(n-1):
    if p[i][0] == p[i+1][0] and p[i][1] == p[i+1][1]:
        cnt += 1
    else:
        tmp *= comb.fact[cnt]
        tmp %= MOD
        cnt = 1
tmp *= comb.fact[cnt]
tmp %= MOD
ans -= tmp

ans = comb.fact[n] - ans
ans %= MOD
print(ans)

import io, sys, atexit, os
import math as ma
from decimal import Decimal as dec
from itertools import permutations
from itertools import combinations


def li ():
	return list (map (int, sys.stdin.readline ().split ()))


def num ():
	return map (int, sys.stdin.readline ().split ())


def nu ():
	return int (input ())


def find_gcd ( x, y ):
	while (y):
		x, y = y, x % y
	return x


def lcm ( x, y ):
	gg = find_gcd (x, y)
	return (x * y // gg)


mm = 998244353
yp = 0

def solve ():
	t = 1
	for tt in range (t):
		n=nu()
		a=[]
		p=[]
		q=[]
		fact=[0]*(n+1)
		fact[0]=1
		for i in range(1,n+1):
			fact[i]=(fact[i-1]*i)%mm
		for i in range(n):
			x,y=num()
			p.append(x)
			q.append(y)
			a.append((x,y))
		a.sort()
		p.sort()
		q.sort()
		cc=1
		last=p[0]
		ans1=1
		for i in range(1,n):
			if(last==p[i]):
				cc+=1
			else:
				ans1=(ans1*fact[cc])%mm
				cc=1
				last=p[i]
		ans1 = (ans1 * fact [ cc ]) % mm
		cc=1
		last=q[0]
		ans2=1
		for i in range(1,n):
			if(last==q[i]):
				cc+=1
			else:
				ans2=(ans2*fact[cc])%mm
				cc=1
				last=q[i]
		ans2 = (ans2 * fact [ cc ]) % mm
		ans3=1
		cc=1
		last=a[0]
		for i in range(1,n):
			if(last==a[i]):
				cc+=1
			elif(last[0]>a[i][0] or last[1]>a[i][1]):
				ans3=0
			else:
				ans3=(ans3*fact[cc])%mm
				cc=1
				last=a[i]
		ans3 = (ans3 * fact [ cc ]) % mm
		print((fact[n]-ans1-ans2+ans3+mm)%mm)

def __starting_point():
	solve ()
__starting_point()

def increasing(arr):
    n = len(arr)
    for i in range(n-1):
        if arr[i+1][0] < arr[i][0] or arr[i+1][1] < arr[i][1]:
            return False

    return True

def getFacts(n,mod):
    facts = [1]*n
    for i in range(1,n):
        facts[i] = facts[i-1]*i
        facts[i] %= mod

    return facts

def solve(arr1,arr2,n,mod):
    facts = getFacts(4*10**5+1,mod)
    total = facts[n]
    dist1 = {}
    for i in arr1:
        if i[0] not in list(dist1.keys()):
            dist1[i[0]] = 1
        else:
            dist1[i[0]] += 1

    dist2 = {}
    for i in arr2:
        if i[0] not in list(dist2.keys()):
            dist2[i[0]] = 1
        else:
            dist2[i[0]] += 1

    count = 1
    for i in dist1:
        count *= facts[dist1[i]]
        count = count%mod

    total -= count
    count = 1
    
    for i in dist2:
        count *= facts[dist2[i]]
        count = count%mod

    #print(count)
    total -= count

    arr1.sort()
    if increasing(arr1):
        count = 1
    else:
        count = 0
    dist3 = {}
    for i in arr1:
        if i not in list(dist3.keys()):
            dist3[i] = 1
        else:
            dist3[i] += 1
        
    for i in dist3:
        count *= facts[dist3[i]]
        count = count%mod

    #print(count,total)
    total += count
    total = total%mod
    print(total)
    

def main():
    mod = 998244353
    n = int(input())
    first = []
    second = []
    for i in range(n):
        a,b = list(map(int,input().split()))
        first.append((a,b))
        second.append((b,a))
    
    solve(first,second,n,mod)


main()


import sys
input = sys.stdin.readline
 
mod=998244353
 
n=int(input())
S=[list(map(int,input().split())) for i in range(n)]
 
FACT=[1]
for i in range(1,3*10**5+10):
    FACT.append(FACT[-1]*i%mod)
 
X=[x for x,y in S]
Y=[y for x,y in S]
 
ANS=1
for i in range(1,n+1):
    ANS=ANS*i%mod
 
X.sort()
XA=1
count=1
for i in range(1,n):
    if X[i]==X[i-1]:
        count+=1
    else:
        XA=XA*FACT[count]%mod
        count=1
        
XA=XA*FACT[count]%mod
 
Y.sort()
YA=1
count=1
for i in range(1,n):
    if Y[i]==Y[i-1]:
        count+=1
    else:
        YA=YA*FACT[count]%mod
        count=1
YA=YA*FACT[count]%mod
 
 
S.sort()
SA=1
count=1
for i in range(1,n):
    if S[i][1]<S[i-1][1]:
        SA=0
        break
    
    if S[i]==S[i-1]:
        count+=1
    else:
        SA=SA*FACT[count]%mod
        count=1
 
SA=SA*FACT[count]%mod
 
    
 
print((ANS-XA-YA+SA)%mod)

import sys
input = sys.stdin.readline

n = int(input())
s = [list(map(int, input().split())) for y in range(n)]
MOD = 998244353

fact = [1]*(n+1)
for i in range(1, n+1):
    fact[i] = (fact[i-1] * i) % MOD

a, b = [0]*n, [0]*n

for x, y in s:
    a[x-1] += 1
    b[y-1] += 1

a_count = 1
b_count = 1
for i in range(n):
    a_count *= fact[a[i]]
    a_count %= MOD
    b_count *= fact[b[i]]
    b_count %= MOD

ab_count = 1
count = 1
s.sort()
for i in range(1, n):
    if s[i][1] < s[i-1][1]:
        ab_count = 0
        break

    if s[i] == s[i-1]:
        count += 1
    else:
        ab_count *= fact[count]
        ab_count %= MOD
        count = 1

ab_count *= fact[count]
ab_count %= MOD

print((fact[n] - a_count - b_count + ab_count) % MOD)


import sys
import itertools
input = sys.stdin.readline

def main():
    n = int(input())
    s = []
    A = []
    B = []
    for _ in range(n):
        a, b = list(map(int, input().split()))
        s.append([a, b])
        A.append(a)
        B.append(b)

    A.sort()
    B.sort()
    s.sort()
    ansA = 1
    ansB = 1
    anss = 1
    for key, val in itertools.groupby(A):
        f = len(list(val))
        for i in range(1, f+1):
            ansA *= i
            ansA %= 998244353

    for key, val in itertools.groupby(B):
        f = len(list(val))
        for i in range(1, f+1):
            ansB *= i
            ansB %= 998244353


    for key, val in itertools.groupby(s):
        f = len(list(val))
        for i in range(1, f+1):
            anss *= i
            anss %= 998244353


    flag = 1
    for i in range(n-1):
        if s[i+1][1] < s[i][1] or s[i+1][0] < s[i][0]:
            flag = 0
            break
    if flag == 0:
        anss = 0


    ansn = 1
    for i in range(1, n+1):
        ansn *= i
        ansn %= 998244353

    print((ansn-ansA-ansB+anss)%998244353)

def __starting_point():
    main()

__starting_point()

from sys import stdin
n=int(stdin.readline().strip())
s=[list(map(int,stdin.readline().strip().split())) for i in range(n)]
s1=[s[i][1] for i in range(n)]
s.sort()
s1.sort()
mod=998244353
x=1
ans=1
fact=[0 for i in range(n+1)]
fact[1]=1
for i in range(2,n+1):
    fact[i]=(i*fact[i-1])%mod
for i in range(1,n):
    if s[i][0]==s[i-1][0]:
        x+=1
    else:
        ans=(ans*fact[x])%mod
        x=1
ans=(ans*fact[x])%mod
t=True
for i in range(1,n):
    if s[i][1]<s[i-1][1]:
        t=False
        break
x=1
ans1=1
for i in range(1,n):
    if s1[i]==s1[i-1]:
        x+=1
    else:
        ans1=(ans1*fact[x])%mod
        x=1
ans1=(ans1*fact[x])%mod
if t:
    ans2=1
    x=1
    for i in range(1,n):
        if s[i]==s[i-1]:
            x+=1
        else:
            ans2=(ans2*fact[x])%mod
            x=1
    ans2=(ans2*fact[x])%mod
    ans=ans+ans1-ans2
else:
    ans+=ans1
print((fact[n]-ans)%mod)
    


import sys
mod = 998244353
fact = [1]
for i in range(1, 300001):
    fact.append((fact[-1]*i)%mod)


data = []
num = int(sys.stdin.readline())

l = []
for _ in range(num):
    data.append([int(i) for i in sys.stdin.readline().split()])
    l.append(data[-1][1])

ans = 0
# f.sort()
# last = -1
# tot = 1
# run = 0
# for p in f:
#     if p == last:
#         run += 1
#     else:
#         tot = (tot * fact[run])%mod
#         run = 1
#         last = p
# tot = (tot * fact[run])%mod
# ans += tot

l.sort()
last = -1
tot = 1
run = 0
for p in l:
    if p == last:
        run += 1
    else:
        tot = (tot * fact[run])%mod
        run = 1
        last = p
tot = (tot * fact[run])%mod
ans += tot

data.sort()


f = [i[0] for i in data]

last = -1
tot = 1
run = 0
for p in f:
    if p == last:
        run += 1
    else:
        tot = (tot * fact[run])%mod
        run = 1
        last = p
tot = (tot * fact[run])%mod
ans += tot





sor = True
for i in range(num-1):
    if data[i][1] > data[i+1][1]:
        sor = False
        break

if sor:
    last = [-1, -1]
    tot = 1
    run = 0
    for p in data:
        if p == last:
            run += 1
        else:
            tot = (tot * fact[run])%mod
            run = 1
            last = p
    tot = (tot * fact[run])%mod
    # print(tot)

    ans -= tot 
q = fact[num]
print((q - ans)%998244353)




import sys
mod = 998244353
fact = [1]
for i in range(1, 300001):
    fact.append((fact[-1]*i)%mod)


data = []
num = int(sys.stdin.readline())

l = []
for _ in range(num):
    data.append(tuple([int(i) for i in sys.stdin.readline().split()]))
    l.append(data[-1][1])

ans = 0
# f.sort()
# last = -1
# tot = 1
# run = 0
# for p in f:
#     if p == last:
#         run += 1
#     else:
#         tot = (tot * fact[run])%mod
#         run = 1
#         last = p
# tot = (tot * fact[run])%mod
# ans += tot

l.sort()
last = -1
tot = 1
run = 0
for p in l:
    if p == last:
        run += 1
    else:
        tot = (tot * fact[run])%mod
        run = 1
        last = p
tot = (tot * fact[run])%mod
ans += tot

data.sort()


f = [i[0] for i in data]

last = -1
tot = 1
run = 0
for p in f:
    if p == last:
        run += 1
    else:
        tot = (tot * fact[run])%mod
        run = 1
        last = p
tot = (tot * fact[run])%mod
ans += tot





sor = True
for i in range(num-1):
    if data[i][1] > data[i+1][1]:
        sor = False
        break

if sor:
    last = (-1, -1)
    tot = 1
    run = 0
    for p in data:
        if p == last:
            run += 1
        else:
            tot = (tot * fact[run])%mod
            run = 1
            last = p
    tot = (tot * fact[run])%mod
    # print(tot)

    ans -= tot 
q = fact[num]
print((q - ans)%998244353)




import sys
mod = 998244353
fact = [1]
for i in range(1, 300001):
    fact.append((fact[-1]*i)%mod)


data = []
num = int(sys.stdin.readline())

l = []
for _ in range(num):
    temp = [int(i) for i in sys.stdin.readline().split()]
    data.append((temp[0]<<20) + temp[1])
    l.append(temp[1])

ans = 0
# f.sort()
# last = -1
# tot = 1
# run = 0
# for p in f:
#     if p == last:
#         run += 1
#     else:
#         tot = (tot * fact[run])%mod
#         run = 1
#         last = p
# tot = (tot * fact[run])%mod
# ans += tot

l.sort()
last = -1
tot = 1
run = 0
for p in l:
    if p == last:
        run += 1
    else:
        tot = (tot * fact[run])%mod
        run = 1
        last = p
tot = (tot * fact[run])%mod
ans += tot

data.sort()


f = [i>>20 for i in data]

last = -1
tot = 1
run = 0
for p in f:
    if p == last:
        run += 1
    else:
        tot = (tot * fact[run])%mod
        run = 1
        last = p
tot = (tot * fact[run])%mod
ans += tot

# print(ans, 999)

pow2m1 = (1<<20) - 1

sor = True
for i in range(num-1):
    # print(data[i] & pow2m1 , data[i+1] & pow2m1)
    if data[i] & pow2m1 > data[i+1] & pow2m1:#fix-----------
        sor = False
        break

# print(data)
# print(sor)
if sor:
    last = -1
    tot = 1
    run = 0
    for p in data:
        if p == last:
            run += 1
        else:
            tot = (tot * fact[run])%mod
            run = 1
            last = p
    tot = (tot * fact[run])%mod
    # print(tot)

    ans -= tot 
q = fact[num]
print((q - ans)%998244353)




import collections

n=int(input())
arr_ab=[]
arr_a=[]
arr_b=[]
for _ in range(n):
  a,b=map(int,input().split())
  arr_a.append(a)
  arr_b.append(b)
  arr_ab.append((a,b))
mod=998244353
tmp=1
facts=[1]
for val in range(1,n+1):
  tmp*=val
  tmp%=mod
  facts.append(tmp)
alls=facts[n]
tmp=1
for value in collections.Counter(arr_a).values():
  tmp*=facts[value]
  tmp%=mod
bads_a=tmp
tmp=1
for value in collections.Counter(arr_b).values():
  tmp*=facts[value]
  tmp%=mod
bads_b=tmp
arr_ab=sorted(arr_ab)
flag=True
for i in range(n-1):
  if arr_ab[i][1]<=arr_ab[i+1][1]:
    continue
  else:
    flag=False
    break
if flag==False:
  print((alls-(bads_a+bads_b)+mod)%mod)
else:
  tmp=1
  for val in collections.Counter(arr_ab).values():
    tmp*=facts[val]
    tmp%=mod
  bads_ab=tmp
  print((alls-(bads_a+bads_b-bads_ab)+mod)%mod)

import collections

n=int(input())
arr_ab=[]
arr_a=[]
arr_b=[]
for _ in range(n):
  a,b=map(int,input().split())
  arr_a.append(a)
  arr_b.append(b)
  arr_ab.append((a,b))
mod=998244353
tmp=1
facts=[1]
for val in range(1,n+1):
  tmp*=val
  tmp%=mod
  facts.append(tmp)
alls=facts[n]
tmp=1
for value in collections.Counter(arr_a).values():
  tmp*=facts[value]
  tmp%=mod
bads_a=tmp
tmp=1
for value in collections.Counter(arr_b).values():
  tmp*=facts[value]
  tmp%=mod
bads_b=tmp
arr_ab=sorted(arr_ab)
flag=True
for i in range(n-1):
  if arr_ab[i][1]<=arr_ab[i+1][1]:
    continue
  else:
    flag=False
    break
if flag==False:
  print((alls-(bads_a+bads_b))%mod)
else:
  tmp=1
  for val in collections.Counter(arr_ab).values():
    tmp*=facts[val]
    tmp%=mod
  bads_ab=tmp
  print((alls-(bads_a+bads_b-bads_ab))%mod)

from collections import defaultdict
import math


MOD = 998244353


def facmod(n):
    fac = 1
    for i in range(2, n + 1):
        fac *= i
        fac %= MOD
    return fac


def main():
    n = int(input())
    s = [tuple(int(s) for s in input().split()) for _ in range(n)]
    
    count = defaultdict(int)

    for a, _ in s:
        count[a] += 1

    first = 1
    for c in list(count.values()):
        first *= facmod(c)
        first %= MOD

    count = defaultdict(int)

    for _, b in s:
        count[b] += 1

    second = 1
    for c in list(count.values()):
        second *= facmod(c)
        second %= MOD

    count = defaultdict(int)
    
    for t in s:
        count[t] += 1
    
    both = 1
    for c in list(count.values()):
        both *= facmod(c)
        both %= MOD

    s.sort()

    for i, (_, b) in enumerate(s[:-1]):
        if b > s[i + 1][1]:
            both = 0
            break
    
    print((facmod(n) - (first + second - both)) % MOD)


def __starting_point():
    main()

__starting_point()