{"trustable":true,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript type\u003d\"text/javascript\" async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS_HTML-full\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eYou 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:\u003c/p\u003e\u003cul\u003e \u003cli\u003e $$$s \u003d [(1, 2), (3, 2), (3, 1)]$$$ is bad because the sequence of first elements is sorted: $$$[1, 3, 3]$$$; \u003c/li\u003e\u003cli\u003e $$$s \u003d [(1, 2), (3, 2), (1, 2)]$$$ is bad because the sequence of second elements is sorted: $$$[2, 2, 2]$$$; \u003c/li\u003e\u003cli\u003e $$$s \u003d [(1, 1), (2, 2), (3, 3)]$$$ is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; \u003c/li\u003e\u003cli\u003e $$$s \u003d [(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. \u003c/li\u003e\u003c/ul\u003e\u003cp\u003eCalculate the number of permutations of size $$$n$$$ such that after applying this permutation to the sequence $$$s$$$ it turns into a good sequence. \u003c/p\u003e\u003cp\u003eA 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 \u003d [(1, 2), (1, 3), (2, 3)]$$$ and $$$p \u003d [2, 3, 1]$$$ then $$$s$$$ turns into $$$[(1, 3), (2, 3), (1, 2)]$$$.\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe first line contains one integer $$$n$$$ ($$$1 \\le n \\le 3 \\cdot 10^5$$$).\u003c/p\u003e\u003cp\u003eThe 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.\u003c/p\u003e\u003cp\u003e\u003cspan class\u003d\"tex-font-style-bf\"\u003eThe sequence $$$s$$$ may contain equal elements\u003c/span\u003e.\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003ePrint 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).\u003c/p\u003e"}},{"title":"Examples","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3\n1 1\n2 2\n3 1\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e3\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e4\n2 3\n2 2\n2 1\n2 4\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e0\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3\n1 1\n1 1\n2 3\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e4\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Note","value":{"format":"HTML","content":"\u003cp\u003eIn first test case there are six permutations of size $$$3$$$: \u003c/p\u003e\u003col\u003e \u003cli\u003e if $$$p \u003d [1, 2, 3]$$$, then $$$s \u003d [(1, 1), (2, 2), (3, 1)]$$$ — bad sequence (sorted by first elements); \u003c/li\u003e\u003cli\u003e if $$$p \u003d [1, 3, 2]$$$, then $$$s \u003d [(1, 1), (3, 1), (2, 2)]$$$ — bad sequence (sorted by second elements); \u003c/li\u003e\u003cli\u003e if $$$p \u003d [2, 1, 3]$$$, then $$$s \u003d [(2, 2), (1, 1), (3, 1)]$$$ — good sequence; \u003c/li\u003e\u003cli\u003e if $$$p \u003d [2, 3, 1]$$$, then $$$s \u003d [(2, 2), (3, 1), (1, 1)]$$$ — good sequence; \u003c/li\u003e\u003cli\u003e if $$$p \u003d [3, 1, 2]$$$, then $$$s \u003d [(3, 1), (1, 1), (2, 2)]$$$ — bad sequence (sorted by second elements); \u003c/li\u003e\u003cli\u003e if $$$p \u003d [3, 2, 1]$$$, then $$$s \u003d [(3, 1), (2, 2), (1, 1)]$$$ — good sequence. \u003c/li\u003e\u003c/ol\u003e"}}]}