Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter


def check(s1, s2, i, j, memo):
    if memo[i] == -1:
        if memo[j] == -1:
            if s1 == s2:
                memo[i] = i
                memo[j] = i
                return 1
            else:
                return 0
        else:
            return 0
    else:
        if memo[j] == -1:
            if s1 == s2:
                memo[j] = i
                return 1
        elif memo[i] == memo[j]:
            return 1

    return 0


def main():
    N = int(input())

    S = []
    for _ in range(N):
        s = input()
        S.append(Counter(s))

    ans = 0
    memo = [-1 for i in range(N)]
    for i in range(N):
        for j in range(i+1, N):
            if len(S[i]) == len(S[j]):
                ans += check(S[i], S[j], i, j, memo)

    print(ans)


main()

Fixed submission:
import math


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n = int(input())
ans_dict = {}

for _ in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    try:
        ans_dict[s] += 1
    except:
        ans_dict[s] = 1

ans = 0
for key in ans_dict:
    if ans_dict[key] >= 2:
        ans += combinations_count(ans_dict[key], 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter


def check(s1, s2, i, j, memo):
    if memo[i] == memo[j] and memo[i] != -1:
        return 1
    elif memo[i] != memo[j] and memo[j] != -1:
        return 0

    if s1 == s2:
        if memo[i] == -1:
            memo[i] = i
            memo[j] = i
        elif memo[j] == -1:
            memo[j] = memo[i]
        return 1

    else:
        return 0


def main():
    N = int(input())

    S = []
    for _ in range(N):
        s = input()
        S.append(Counter(s))

    ans = 0
    memo = [-1 for i in range(N)]
    for i in range(N):
        for j in range(i+1, N):
            if len(S[i]) == len(S[j]):
                ans += check(S[i], S[j], i, j, memo)

    print(ans)


main()

Fixed submission:
import math


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n = int(input())
ans_dict = {}

for _ in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    try:
        ans_dict[s] += 1
    except:
        ans_dict[s] = 1

ans = 0
for key in ans_dict:
    if ans_dict[key] >= 2:
        ans += combinations_count(ans_dict[key], 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter


def check(s1, s2, i, j, memo):
    if memo[i] == memo[j] and memo[i] != -1:
        return 1
    elif memo[i] != memo[j] and memo[j] != -1:
        return 0

    if s1 == s2:
        if memo[i] == -1:
            memo[i] = i
            memo[j] = i
        elif memo[j] == -1:
            memo[j] = memo[i]
        return 1

    else:
        return 0


def main():
    N = int(input())

    S = []
    for _ in range(N):
        s = input()
        S.append(Counter(s))

    ans = 0
    memo = [-1 for i in range(N)]
    for i in range(N):
        for j in range(i+1, N):
            ans += check(S[i], S[j], i, j, memo)

    print(ans)


main()

Fixed submission:
import math


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n = int(input())
ans_dict = {}

for _ in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    try:
        ans_dict[s] += 1
    except:
        ans_dict[s] = 1

ans = 0
for key in ans_dict:
    if ans_dict[key] >= 2:
        ans += combinations_count(ans_dict[key], 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter


def check(s1, s2, i, j, memo):
    if memo[i] == memo[j] and memo[i] != -1:
        return 1
    elif memo[i] != memo[j] and memo[j] != -1:
        return 0

    s1_cnt = Counter(s1)
    s2_cnt = Counter(s2)

    if s1_cnt == s2_cnt:
        if memo[i] == -1:
            memo[i] = i
            memo[j] = i
        elif memo[j] == -1:
            memo[j] = memo[i]
        return 1

    else:
        return 0



def main():
    N = int(input())

    S = []
    for _ in range(N):
        S.append(input())

    ans = 0
    memo = [-1 for i in range(N)]
    for i in range(N):
        for j in range(i+1, N):
            ans += check(S[i], S[j], i, j, memo)

    print(ans)


main()

Fixed submission:
import math


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n = int(input())
ans_dict = {}

for _ in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    try:
        ans_dict[s] += 1
    except:
        ans_dict[s] = 1

ans = 0
for key in ans_dict:
    if ans_dict[key] >= 2:
        ans += combinations_count(ans_dict[key], 2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter


def check(s1, s2):
    s1_cnt = Counter(s1)
    s2_cnt = Counter(s2)
    if s1_cnt == s2_cnt:
        return 1
    else:
        return 0


N = int(input())

S = []
for _ in range(N):
    S.append(input())

ans = 0
for i in range(N):
    for j in range(i+1, N):
       ans += check(S[i], S[j])

print(ans)


Fixed submission:
import math


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n = int(input())
ans_dict = {}

for _ in range(n):
    s = list(input())
    s.sort()
    s = ''.join(s)
    try:
        ans_dict[s] += 1
    except:
        ans_dict[s] = 1

ans = 0
for key in ans_dict:
    if ans_dict[key] >= 2:
        ans += combinations_count(ans_dict[key], 2)
print(ans)