Problem p02732 (python)

Fixed submission:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int, readline().split()))

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1

total = 0 
diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = c * (c - 1) // 2
    total += x
    if c - 1 <= 0:
        diff[i] = x
    else:
        y = (c - 1) * (c - 2) // 2
        diff[i] = x - y

for i in range(n):
    print(total - diff[a[i]])

Buggy submission (verdict: Runtime Error):
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int, readline().split()))

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1

MAX = 10 ** 5
MOD = 10 ** 9 + 7
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]

def comInit(mod):
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod

def com(n, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod

comInit(MOD)

# total = 0 
# diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = com(c, 2, MOD)
#     total += x
#     if c - 1 <= 0:
#         diff[i] = x
#     else:
#         y = com(c - 1, 2, MOD)
#         diff[i] = x - y

# for i in range(n):
#     print(total - diff[a[i]])

Buggy submission (verdict: Runtime Error):
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int, readline().split()))

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1

MAX = 10 ** 5
MOD = 10 ** 9 + 7
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]

def comInit(mod):
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod

def com(n, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod
comInit(MOD)

total = 0 
diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = com(c, 2, MOD)
    total += x
    # if c - 1 <= 0:
    #     diff[i] = x
    # else:
    #     y = com(c - 1, 2, MOD)
    #     diff[i] = x - y

for i in range(n):
    print(total - diff[a[i]])

Buggy submission (verdict: Runtime Error):
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int, readline().split()))

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1


MAX = 10 ** 5
MOD = 10 ** 9 + 7
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]

def comInit(mod):
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod


def com(n, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod
comInit(MOD)

total = 0 
diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = com(c, 2, MOD)
    total += x
    if c - 1 <= 0:
        diff[i] = x
    else:
        y = com(c - 1, 2, MOD)
        diff[i] = x - y

for i in range(n):
    print(total - diff[a[i]])

Buggy submission (verdict: Runtime Error):
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int, readline().split()))

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1


MAX = 10 ** 5
MOD = 10 ** 9 + 7
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]

def comInit(mod):
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod


def com(n, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod
comInit(MOD)

total = 0 
diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = com(c, 2, MOD)
    total += x
    y = com(c - 1, 2, MOD)
    diff[i] = x - y

for i in range(n):
    print(total - diff[a[i]])