Problem p02679 (python)

Fixed submission:
import sys
from collections import Counter
from math import gcd


MOD = 10 ** 9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


def profile(fish):
    x, y = fish
    GCD = gcd(abs(x), abs(y))

    return (abs(x // GCD), abs(y // GCD), int((x >= 0) ^ (y >= 0)))


N = int(input())
F = [tuple(map(int, s.split())) for s in sys.stdin.readlines()]
modFacts, invs = prepare(N)

C0 = Counter()
C1 = Counter()
zero = 0
for f in F:
    if f == (0, 0):
        zero += 1
    else:
        x, y, flag = profile(f)
        if x == 0:
            C0[(0, 1, 0)] += 1
            C1[(0, 1, 0)] += 1
        elif y == 0:
            C0[(0, 1, 0)] += 1
            C1[(1, 0, 1)] += 1
        else:
            C1[(x, y, flag)] += 1
            if flag:
                x, y = y, x
                flag = 0
            C0[(x, y, flag)] += 1

ans = 1
for x, y, flag in C0.keys():
    b = C1[(y, x, flag ^ 1)]
    a = C0[(x, y, flag)] - b
    cnt = (pow(2, a, MOD) - 1) + (pow(2, b, MOD) - 1) + 1
    ans *= cnt
    ans %= MOD
ans -= 1
ans += zero
print(ans % MOD)

Buggy submission (verdict: Runtime Error):
import sys
from collections import Counter
from math import gcd


MOD = 10 ** 9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


def profile(fish):
    x, y = fish
    GCD = gcd(abs(x), abs(y))

    return (abs(x // GCD), abs(y // GCD), int((x >= 0) ^ (y >= 0)))


N = int(input())
F = [tuple(map(int, s.split())) for s in sys.stdin.readlines()]
modFacts, invs = prepare(N)
B = [tuple(profile(f)) for f in F]

C0 = Counter()
C1 = Counter()
zero = 0
for f in F:
    if f == (0, 0):
        zero += 1
    else:
        x, y, flag = profile(f)
        if x == 0:
            C0[(0, 1, 0)] += 1
            C1[(0, 1, 0)] += 1
        elif y == 0:
            C0[(0, 1, 0)] += 1
            C1[(1, 0, 1)] += 1
        else:
            C1[(x, y, flag)] += 1
            if flag:
                x, y = y, x
                flag = 0
            C0[(x, y, flag)] += 1

ans = 1
for x, y, flag in C0.keys():
    b = C1[(y, x, flag ^ 1)]
    a = C0[(x, y, flag)] - b
    cnt = (pow(2, a, MOD) - 1) + (pow(2, b, MOD) - 1) + 1
    ans *= cnt
    ans %= MOD
ans -= 1
print(ans % MOD)

Buggy submission (verdict: Runtime Error):
import sys
from collections import Counter
from math import gcd


MOD = 10 ** 9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


def profile(fish):
    x, y = fish
    GCD = gcd(abs(x), abs(y))

    return (abs(x // GCD), abs(y // GCD), int((x >= 0) ^ (y >= 0)))


N = int(input())
F = [tuple(map(int, s.split())) for s in sys.stdin.readlines()]
modFacts, invs = prepare(N)
B = [tuple(profile(f)) for f in F]

C0 = Counter()
C1 = Counter()
zero = 0
for f in F:
    x, y, flag = profile(f)
    if (x, y) == 0:
        zero += 1
    elif x == 0:
        C0[(0, 1, 0)] += 1
        C1[(0, 1, 0)] += 1
    elif y == 0:
        C0[(0, 1, 0)] += 1
        C1[(1, 0, 1)] += 1
    else:
        C1[(x, y, flag)] += 1
        if flag:
            x, y = y, x
            flag = 0
        C0[(x, y, flag)] += 1

ans = 1
for x, y, flag in C0.keys():
    b = C1[(y, x, flag ^ 1)]
    a = C0[(x, y, flag)] - b
    cnt = (pow(2, a, MOD) - 1) + (pow(2, b, MOD) - 1) + 1
    ans *= cnt
    ans %= MOD
ans -= 1
print(ans % MOD)