Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left #bisect_left(list, value)
#from fractions import gcd
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

K = int(input())
d = defaultdict(int)

if K<10:
    print(K)
    exit()

# i: 0~99
# j: 0~8
dp = [[0]*10 for _ in range(20)]
for i in range(10):
    dp[0][i] = 1

end = False
# morau DP
i = 1
cnt = 9
while 1:
    #print('i: ', i)
    #pri(dp)
    for j in range(10):
        #print('j: ', j)
        if end:
            break
        if j==0:
            dp[i][j] += dp[i-1][j]
            dp[i][j] += dp[i-1][j+1]
        elif j==9:
            dp[i][j] += dp[i-1][j-1]
            dp[i][j] += dp[i-1][j]
        else:
            dp[i][j] += dp[i-1][j-1]
            dp[i][j] += dp[i-1][j]
            dp[i][j] += dp[i-1][j+1]

        if j!=0:
            addval = dp[i][j]
        else:
            addval = 0

        #print(' i, j, cnt, addval:', i, j, cnt, addval)
        if cnt+addval>=K:
        #if dp[i][j]>=K and j!=0:
            end = True
            break
        else:
            cnt += addval
    if end:
        break
    i += 1

#pri(dp)
#print(i, j, cnt)

def check(i, val): # i+1 桁の数が来る
    sval = str(val)
    for k in range(i):
        aa = abs(int(sval[k])-int(sval[k+1]))
        if aa>1:
            return False
    else:
        return True

val = 10**i*j
while cnt<K:
    #print(' val:', val)
    if check(i, val):
        cnt += 1
    val += 1
        
print(val-1)




Fixed submission:
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

K = int(input())
d = deque()
for i in range(1, 10):
    d.append(i)

def myappend(n):
    sn = str(n)
    nlast = n%10
    if nlast == 0:
        d.append(10*n + nlast)
        d.append(10*n + nlast + 1)
    elif nlast == 9:
        d.append(10*n + nlast - 1)
        d.append(10*n + nlast)
    else:
        d.append(10*n + nlast - 1)
        d.append(10*n + nlast)
        d.append(10*n + nlast + 1)

cnt = 0
while d:
    val = d.popleft()
    cnt += 1
    if cnt == K:
        print(val)
        exit()
    myappend(val)
Buggy python submission (verdict: Runtime Error):
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))
def prime_decomposition(n):
    i = 2
    table = []
    while i*i <= n:
        while n%i == 0:
            n //= i
            table.append(i)
        i += 1
    if n > 1:
        table.append(n)
    return table
def prime_decomposition2(n):
    i = 2
    table = defaultdict(int)
    while i*i <= n:
        while n%i == 0:
            n //= i
            table[i] += 1
        i += 1
    if n > 1:
        table[n] += 1
    return table
def make_divisor(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n%i == 0:
            divisors.append(i)
            if i != n//i:
                divisors.append(n//i)
    return divisors

N = int(input())
list_pd1 = make_divisor(N)
list_pd1.sort()
dict_pd2 = prime_decomposition2(N-1)
#print(N, ':', list_pd1)
#print(N-1, ':', dict_pd2)

cnt = 1

# -1 nohou
for val in dict_pd2.values():
    cnt *= (val+1)
cnt -= 1
#print(cnt)

for k in list_pd1[1:]:
    #print('k:', k)
    sN = N
    while sN >= k:
        if sN%k==0:
            sN //= k
        else:
            sN %= k
    if sN == 1:
        cnt += 1

print(cnt)

Fixed submission:
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))
def prime_decomposition(n):
    i = 2
    table = []
    while i*i <= n:
        while n%i == 0:
            n //= i
            table.append(i)
        i += 1
    if n > 1:
        table.append(n)
    return table
def prime_decomposition2(n):
    i = 2
    table = defaultdict(int)
    while i*i <= n:
        while n%i == 0:
            n //= i
            table[i] += 1
        i += 1
    if n > 1:
        table[n] += 1
    return table
def make_divisor(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n%i == 0:
            divisors.append(i)
            if i != n//i:
                divisors.append(n//i)
    return divisors

N = int(input())
list_pd1 = make_divisor(N)
list_pd1.sort()
dict_pd2 = prime_decomposition2(N-1)
#print(N, ':', list_pd1)
#print(N-1, ':', dict_pd2)

cnt = 1

# -1 nohou
for val in dict_pd2.values():
    cnt *= (val+1)
cnt -= 1
#print(cnt)

for k in list_pd1[1:]:
    #print('k:', k)
    sN = N
    while sN >= k:
        if sN%k==0:
            sN //= k
        else:
            sN %= k
    if sN == 1:
        cnt += 1

print(cnt)
Buggy python submission (verdict: Runtime Error):
S, T = input.split()
a, b = map(int, input.split())
s = input()
if s == S:
  print(a-1, b)
else:
  print(a, b-1)


Fixed submission:
import sys, math
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter
from math import factorial

sys.setrecursionlimit(10**7)
S, T = input().split()
A, B = map(int, input().split())
s = input()

if s == S:
    print(A-1, B)
else:
    print(A, B-1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys, math
from time import sleep
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from fractions import gcd
e = enumerate

sys.setrecursionlimit(10**7)
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

dp = [[float('inf')]*(H+1) for _ in range(N+1)]
#for i in range(H+1):
#    dp[0][i] = 0

#for i in range(N+1):
#    print(dp[i])


for i, ab in e(AB):
    a, b = ab[0], ab[1]
    for j in range(H+1):

        if j==0:
            newval = b
        elif j%a==0:
            newval = j//a * b
        else:
            newval = (j//a+1) * b

        if j>=a:
            newval2 = dp[i][j-a] + b
        else:
            newval2 = float('inf')

        if j>=a:
            newval3 = dp[i+1][j-a] + b
        else:
            newval3 = float('inf')

        dp[i+1][j] = min(dp[i][j], newval, newval2, newval3)

#    print(dp[i+1])
#    sleep(3)
#for i in range(N+1):
#    print(dp[i])

print(dp[N][H])


Fixed submission:
import sys, math
from time import sleep
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from fractions import gcd
e = enumerate

sys.setrecursionlimit(10**7)
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

dp = [[float('inf')]*(H+1) for _ in range(N+1)]


for i, ab in e(AB):
    a, b = ab[0], ab[1]
    for j in range(H+1):

        if j==0:
            newval = b
        elif j%a==0:
            newval = j//a * b
        else:
            newval = (j//a+1) * b

        if j>=a:
            newval2 = dp[i][j-a] + b
        else:
            newval2 = float('inf')

        if j>=a:
            newval3 = dp[i+1][j-a] + b
        else:
            newval3 = float('inf')

        dp[i+1][j] = min(dp[i][j], newval, newval2, newval3)


print(dp[N][H])
Buggy python submission (verdict: Time Limit Exceeded):
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter
from math import factorial
from bisect import bisect_right

sys.setrecursionlimit(10**7)
N = int(input())
P = list(map(int, input().split()))

rP = list(reversed(P))

cnt = 0
for i, rp in enumerate(rP):
    for val in rP[i+1:]:
        if rp > val:
            break
    else:
        cnt += 1

print(cnt)


Fixed submission:
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter
from math import factorial
from bisect import bisect_right

sys.setrecursionlimit(10**7)
N = int(input())
P = list(map(int, input().split()))

cnt = 0
minv = P[0]
for p in P:
    minv = min(minv, p)
    if p == minv:
        cnt += 1

print(cnt)
Buggy python submission (verdict: Runtime Error):
import math
import itertools
import sys

x, y = [int(x) for x in input().split()]

if (x+y) % 3:
    print(0)
    sys.exit()

total_move = (x+y) // 3
move_a = 2 * total_move - x

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = total_move
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

    a = cmb(total_move,move_a,mod)

print(a)

Fixed submission:
import math
import itertools
import sys

x, y = [int(x) for x in input().split()]

if (x+y) % 3:
  print(0)
  sys.exit()

total_move = (x+y) // 3
move_a = 2 * total_move - x

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = total_move
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

a = cmb(total_move,move_a,mod)

print(a)
Buggy python submission (verdict: Runtime Error):
import math
import sys
#input = sys.stdin.readline
sys.setrecursionlimit(10**7)
a, b, x = map(int, input().split())

if a*a*b/2 < x:
    l = 2*b - 2*x/(a*a)
    print(90 - math.degrees(math.atan(a/l)))

elif a*a*b/2 == x:
    print(90 - math.degrees(math.atan(a/b)))

else:
    l = 2*x / (a*b)
    print(90 - math.degrees(math.atan(l/b)))


Fixed submission:
import math
import sys
#input = sys.stdin.readline
sys.setrecursionlimit(10**7)
a, b, x = map(int, input().split())

if a*a*b == x:
	print(0)

elif a*a*b/2 < x:
    l = 2*b - 2*x/(a*a)
    print(90 - math.degrees(math.atan(a/l)))

elif a*a*b/2 == x:
    print(90 - math.degrees(math.atan(a/b)))

else:
    l = 2*x / (a*b)
    print(90 - math.degrees(math.atan(l/b)))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
N, M, L = map(int, input().split())
ABC = [list(map(int, input().split())) for _ in range(M)]
Q = int(input())
st = [list(map(int, input().split())) for _ in range(Q)]


def wf(d):
    for k in range(N):
        for i in range(N):
            for j in range(N):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])

d = [[float('inf') for _ in range(N)] for _ in range(N)]

for abc in ABC:
    d[abc[0]-1][abc[1]-1] = abc[2]
    d[abc[1]-1][abc[0]-1] = abc[2]
for i in range(N):
    d[i][i] = 0
wf(d)

for i in range(N):
    for j in range(N):
        if d[i][j] <= L:
            d[i][j] = 1
        else:
            d[i][j] = float('inf')

wf(d)

for sti in st:
    res = d[sti[0]-1][sti[1]-1]
    if res == float('inf'):
        print(-1)
    else:
        print(res-1)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
N, M, L = map(int, input().split())
ABC = [list(map(int, input().split())) for _ in range(M)]
Q = int(input())
st = [list(map(int, input().split())) for _ in range(Q)]


def wf(d):
    for k in range(N):
        for i in range(N):
            for j in range(N):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])

d = [[float('inf') for _ in range(N)] for _ in range(N)]

for abc in ABC:
    d[abc[0]-1][abc[1]-1] = abc[2]
    d[abc[1]-1][abc[0]-1] = abc[2]
for i in range(N):
    d[i][i] = 0
wf(d)

for i in range(N):
    for j in range(N):
        if d[i][j] <= L:
            d[i][j] = 1
        else:
            d[i][j] = float('inf')

wf(d)

for sti in st:
    res = d[sti[0]-1][sti[1]-1]
    if res == float('inf'):
        print(-1)
    else:
        print(res-1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
S = input()
K = int(input())

lenS = len(S)
if lenS == 1:
    print(K//2)
    exit()

cnt_one = 0
i = 0
while i < lenS-1:
    if S[i] == S[i+1]:
        cnt_one += 1
        i += 2
    else:
        i += 1

if K == 1:
    print(cnt_one)
    exit()

SS = S*2
lenS = len(SS)

cnt_two = 0
i = 0
while i < lenS-1:
    if SS[i] == SS[i+1]:
        cnt_two += 1
        i += 2
    else:
        i += 1

lenS = len(S)
if len(set(S)) == 1:
    if lenS%2 == 1:
        ans = 0
        for j in range(1, K+1):
            if j%2 == 1:
                ans += cnt_one
            else:
                ans += cnt_two - cnt_one
        print(ans)
        exit()

print((cnt_two - cnt_one)*(K-1) + cnt_one)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
S = input()
K = int(input())

lenS = len(S)
if lenS == 1:
    print(K//2)
    exit()

cnt_one = 0
i = 0
while i < lenS-1:
    if S[i] == S[i+1]:
        cnt_one += 1
        i += 2
    else:
        i += 1

if K == 1:
    print(cnt_one)
    exit()

SS = S*2
lenSS = len(SS)

cnt_two = 0
i = 0
while i < lenSS-1:
    if SS[i] == SS[i+1]:
        cnt_two += 1
        i += 2
    else:
        i += 1

if len(set(S)) == 1:
    if lenS%2 == 1:
        if K%2 == 0:
            ans = (cnt_two) * (K//2)
        else:
            ans = (cnt_one * (K//2 + 1)) + ((cnt_two-cnt_one) * (K//2))
        print(ans)
        exit()

print((cnt_two - cnt_one)*(K-1) + cnt_one)
Buggy python submission (verdict: Runtime Error):
sin/agc039 >>> cat b.py 
from collections import deque
import sys
sys.setrecursionlimit(10**7)
N = int(input())
S = [input().strip() for _ in range(N)]
C = [0]*N

flg = False
for sp, row in enumerate(S):
    if flg:
        break

    if row.count('1') == 1 or sp == len(S)-1:
        flg = True

        # start BFS
        C[sp] = 1
        q = deque([])
        for i, v in enumerate(row):
            if v == '1':
                q.append(int(i))

        cnt = 0 # length
        c = -1 # color
        while q:
            cnt += 1
            for i in range(len(q)):
                np_ind = q.popleft()
                if C[np_ind] == -c:
                    print(-1)
                    exit()
                elif C[np_ind] == c:
                    pass
                else:
                    C[np_ind] = c
                    for i, v in enumerate(S[np_ind]):
                        if v == '1':
                            q.append(int(i))
            c *= -1

print(cnt)

Fixed submission:
from collections import deque
import sys
sys.setrecursionlimit(10**7)
N = int(input())
S = [input().strip() for _ in range(N)]

flg = False
max_cnt = 1
for sp, row in enumerate(S):
    # start BFS
    C = [0]*N
    C[sp] = 1
    q = deque([])
    for i, v in enumerate(row):
        if v == '1':
            q.append(int(i))

    cnt = 1 # length
    c = -1 # color
    while q:
        for i in range(len(q)):
            np_ind = q.popleft()
            if C[np_ind] == -c:
                print(-1)
                exit()
            elif C[np_ind] == c:
                pass
            else:
                C[np_ind] = c
                for i, v in enumerate(S[np_ind]):
                    if v == '1' and C[i] == 0:
                        q.append(int(i))
        cnt += 1
        c *= -1
    if max_cnt < cnt:
        max_cnt = cnt

print(max_cnt)
Buggy python submission (verdict: Runtime Error):
import math
import sys
input = sys.stdin.readline
A, B = map(int, input().split())

gcdv = math.gcd(A, B)

def fac(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr

ans = len(fac(gcdv))
if ans == 1:
    print(1)
else:
    print(ans+1)

Fixed submission:
import sys
input = sys.stdin.readline
A, B = map(int, input().split())

def gcd(a, b):
    if b==0:
        return a
    else:
        return gcd(b, a%b)

def fac(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr

gcdv = gcd(A, B)
l = fac(gcdv)
ans = len(l)

if ans == 1:
    if l[0][0] == 1:
        print(1)
    else:
        print(ans+1)
else:
    print(ans+1)
Buggy python submission (verdict: Time Limit Exceeded):
from math import log2
N, M = map(int, input().split())
A = list(map(int, input().split()))

for i in range(M):
    A.sort()
    A[-1] = (A[-1]//2)

print(sum(A))


Fixed submission:
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))

h = []
for a in A:
    heapq.heappush(h, -a)

for i in range(M):
    new = heapq.heappop(h)
    new = -((-new)//2)
    heapq.heappush(h, new)

print(-sum(h))
Buggy python submission (verdict: Runtime Error):
N = int(input())

lN = list(range(1, N+1))
pN = list(range(2, N+1))
pN.append(1)

res = 0
for ln, pn in zip(lN, pN):
    res += (ln%pn)

print(res)

Fixed submission:
N = int(input())
res = ((N-1)*N)//2
print(res)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import time
def input():
    return sys.stdin.readline()[:-1]
 
def main():
    N = int(input())
    A = [list(map(int, input().split())) for _ in range(N)]
    l = [0] * N
    ans = 0
    count = 0
    check = [False] * N
    tmpl = []
    next = False
 
    while(count < N):
        check = [False] * N
        for i in range(N):
            if l[i] < N - 1:
                to = A[i][l[i]] - 1
                if (not check[to]) and (not check[i]):
                    if A[to][l[to]] - 1 == i:
                        l[i] += 1
                        l[to] += 1
                        check[to] = True
                        check[i] = True
                        next = True
                    if l[i] == N - 1:
                        count += 1
                    if l[to] == N - 1:
                        count += 1
        ans += 1
        if not next:
            print(-1)
            exit()
        next = False
 
    print(ans)
 
if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import defaultdict
from itertools import product, permutations, combinations

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]

q = list(range(N))
c = defaultdict(int)
t = [0]*N
r = 0

while q:
    r += 1
    nq = []
    for i in q:
        j = A[i][t[i]] - 1
        k = i
        if k < j:
            j, k = k, j
        c[(j, k)] += 1
        if c[(j, k)] == 2:
            t[j] += 1
            t[k] += 1
            if t[j] < N-1:
                nq.append(j)
            if t[k] < N-1:
                nq.append(k) 
    q = nq

for tt in t:
    if tt != N-1:
        print(-1)
        exit()

print(r)
Buggy python submission (verdict: Runtime Error):
from collections import deque
N, Q = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N-1)]
PX = [list(map(int, input().split())) for _ in range(Q)]

chil = [[] for _ in range(N+1)]
count = [0]*(N+1)

for ab in AB:
    chil[ab[0]].append(ab[1])
    chil[ab[1]].append(ab[0])

def cleanse(fromN, toN):
    chil[toN].remove(fromN)
    if len(chil[toN])==0:  # leaf
        return
    for node in chil[toN]:
        cleanse(toN, node)

for node in chil[1]:
    cleanse(1, node)

def addition(fromN, toN, value):
    count[toN] += value
    if len(chil[toN])==0:
        return
    for node in chil[toN]:
        addition(toN, node, value)

for px in PX:
    count[px[0]] += px[1]
    for node in chil[px[0]]:
        addition(px[0], node, px[1])

print(*count[1:])

Fixed submission:
a = int(input())
s = input()

if a>=3200: print(s)
else: print('red')
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = [input() for _ in range(N)]

col_S = [Counter(s) for s in S]
check_S = [0]*len(S)

res = 0
for ind, col_s in enumerate(col_S):
    if check_S[ind] == 1: continue
    cnt = 1
    for ref_ind, ref_col_s in enumerate(col_S[ind+1:]):
        if col_s == ref_col_s:
            check_S[ind+ref_ind+1] = 1
            cnt += 1
    res += cnt * (cnt-1) / 2

print(int(res))

Fixed submission:
N = int(input())
lS = [list(input()) for _ in range(N)]
S = [''.join(sorted(s)) for s in lS]
S = sorted(S)

cnt = 1
S.append('end')
last = S[0]
res = 0
for s in S[1:]:
    if last == s:
        cnt += 1
    else:
        res += cnt * (cnt-1) /2
        last = s
        cnt = 1

print(int(res))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = [input() for _ in range(N)]

col_S = [Counter(s) for s in S]

cnt = 0
for ind, col_s in enumerate(col_S):
    for ref_ind, ref_col_s in enumerate(col_S[ind+1:]):
        if col_s == ref_col_s: cnt += 1

print(cnt)

Fixed submission:
N = int(input())
lS = [list(input()) for _ in range(N)]
S = [''.join(sorted(s)) for s in lS]
S = sorted(S)

cnt = 1
S.append('end')
last = S[0]
res = 0
for s in S[1:]:
    if last == s:
        cnt += 1
    else:
        res += cnt * (cnt-1) /2
        last = s
        cnt = 1

print(int(res))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = [input() for _ in range(N)]

col_S = [Counter(s) for s in S]

cnt = 0
for ind, col_s in enumerate(col_S):
    for ref_ind, ref_col_s in enumerate(col_S[ind+1:]):
        if col_s == ref_col_s: cnt += 1

print(cnt)

Fixed submission:
N = int(input())
lS = [list(input()) for _ in range(N)]
S = [''.join(sorted(s)) for s in lS]
S = sorted(S)

cnt = 1
S.append('end')
last = S[0]
res = 0
for s in S[1:]:
    if last == s:
        cnt += 1
    else:
        res += cnt * (cnt-1) /2
        last = s
        cnt = 1

print(int(res))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = [input() for _ in range(N)]

col_S = [Counter(s) for s in S]

cnt = 0
for ind, col_s in enumerate(col_S):
    for ref_ind, ref_col_s in enumerate(col_S[ind+1:]):
        if col_s == ref_col_s: cnt += 1

print(cnt)

Fixed submission:
N = int(input())
lS = [list(input()) for _ in range(N)]
S = [''.join(sorted(s)) for s in lS]
S = sorted(S)

cnt = 1
S.append('end')
last = S[0]
res = 0
for s in S[1:]:
    if last == s:
        cnt += 1
    else:
        res += cnt * (cnt-1) /2
        last = s
        cnt = 1

print(int(res))