#include #include #include #include #include constexpr int64_t p = 998244353; std::vector factorials; void doCase() { struct Block { Block(int64_t l, int64_t w) : l(l), w(w) { if(l < w) std::swap(this->l, this->w); } bool operator<(const Block &other) const { if(l > other.l) return true; else if(l < other.l) return false; return w > other.w; } int64_t l, w; }; int n; std::cin >> n; std::map blocks; for(int i=0; i> l >> w; Block b = Block(l,w); blocks[b]++; } while(factorials.size() <= n) factorials.push_back( (factorials.back() * factorials.size()) % p); auto it = blocks.begin(); int64_t answer = 1; int64_t prevl = blocks.begin()->first.l; int64_t prevw = blocks.begin()->first.w; if(prevl != prevw) answer = 2; for(auto it : blocks) { answer = (answer * factorials[it.second]) % p; if(it.first.w > prevw) answer = 0; int64_t options = ((prevl - it.first.l + 1) * (prevw - it.first.w + 1)) % p; if(it.first.l != it.first.w && it.first.l <= prevw) { options = (options + (prevl - it.first.w + 1) * (prevw - it.first.l + 1)) % p; } answer = (answer * options) % p; prevl = it.first.l; prevw = it.first.w; } std::cout << answer << std::endl; } int main() { factorials.push_back(1); int T; std::cin >> T; for(int i=0; i