# 2024 # Maciej Szeptuch import sys from heapq import heappush, heappop from bisect import bisect, insort MOD = 1000000007 def combine(perm1, perm2): return tuple(perm1[b - 1] for b in perm2) def inversions(perm): heap = [] count = 0 for i, v in enumerate(perm): heappush(heap, (v, i)) idxs = [] while heap: _, i = heappop(heap) y = bisect(idxs, i) count = (count + i - y) % MOD insort(idxs, i) return count def gcd(a, b): if a == 0: return b, 0, 1 g, x, y = gcd(b % a, a) return g, y - (b // a) * x, x if __name__ == "__main__": data = [int(x) for x in sys.stdin.read().split()] length = data[0] permutations = data[1] favourite = [] combined = set() for p in range(permutations): favourite.append(tuple(data[2 + p * length : 2 + (p + 1) * length])) combined.add(favourite[p]) que = list(combined) while que: elem = que.pop() for permutation in favourite: new_perm = combine(elem, permutation) if new_perm not in combined: combined.add(new_perm) que.append(new_perm) total_inversions = 0 for perm in combined: total_inversions = (total_inversions + inversions(perm)) % MOD print((gcd(len(combined), MOD)[1] * total_inversions) % MOD)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | # 2024 # Maciej Szeptuch import sys from heapq import heappush, heappop from bisect import bisect, insort MOD = 1000000007 def combine(perm1, perm2): return tuple(perm1[b - 1] for b in perm2) def inversions(perm): heap = [] count = 0 for i, v in enumerate(perm): heappush(heap, (v, i)) idxs = [] while heap: _, i = heappop(heap) y = bisect(idxs, i) count = (count + i - y) % MOD insort(idxs, i) return count def gcd(a, b): if a == 0: return b, 0, 1 g, x, y = gcd(b % a, a) return g, y - (b // a) * x, x if __name__ == "__main__": data = [int(x) for x in sys.stdin.read().split()] length = data[0] permutations = data[1] favourite = [] combined = set() for p in range(permutations): favourite.append(tuple(data[2 + p * length : 2 + (p + 1) * length])) combined.add(favourite[p]) que = list(combined) while que: elem = que.pop() for permutation in favourite: new_perm = combine(elem, permutation) if new_perm not in combined: combined.add(new_perm) que.append(new_perm) total_inversions = 0 for perm in combined: total_inversions = (total_inversions + inversions(perm)) % MOD print((gcd(len(combined), MOD)[1] * total_inversions) % MOD) |