1207D - Number Of Permutations - CodeForces Solution


combinatorics *1800

Please click on ads to support us..

Python Code:

from collections import Counter
import sys

input = sys.stdin.readline

MOD = 998244353
n = int(input())
s = sorted([tuple(map(int, input().split())) for _ in range(n)])

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

def ways(l):
    count = Counter(l)
    res = 1
    for x in count.values():
        res = res * fact[x] % MOD
    return res

a = ways([x for x,_ in s])
b = ways([y for _,y in s])
c = ways(s) if all(s[i][1] <= s[i+1][1] for i in range(n-1)) else 0

print((fact[n] - a - b + c) % MOD)


Comments

Submit
0 Comments
More Questions

1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books
1374B - Multiply by 2 divide by 6
1093B - Letters Rearranging
1213C - Book Reading
1468C - Berpizza
1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant