Problem p02679 (python)

Fixed submission:
# WA
import math
n = int(input())
a = [0] * n;  b = [0] * n;
for i in range(n):
    a[i], b[i] = [int(x) for x in input().split()]
p = 10 ** 9 + 7

# d = [0] * n
dict = {}
for i in range(n):
    d = math.gcd(a[i], b[i])
    if d > 0:
        aa = a[i] // d
        bb = b[i] // d
    else:
        aa = a[i]
        bb = b[i]
    if aa < 0: # 非負数にする
        aa = -aa
        bb = -bb
    if aa == 0 and bb > 0:
        bb = -bb
    # print("aa, bb = {}, {}".format(aa, bb))
    if (aa, bb) in dict:
        dict[(aa, bb)] += 1
    else:
        dict[(aa, bb)] = 1

bad = []
others = []
zeros = 0

for (aa, bb) in dict:
    if dict[(aa, bb)] == 0: continue
    if aa == 0 and bb == 0:
        zeros += dict[(aa, bb)]
        dict[(aa, bb)] = 0
    elif (bb, -aa) in dict:
        bad.append((dict[(aa, bb)], dict[(bb, -aa)]))
        dict[(bb, -aa)] = 0
    elif (-bb, aa) in dict:
        bad.append((dict[(aa, bb)], dict[(-bb, aa)]))
        dict[(-bb, aa)] = 0
    else:
        others.append(dict[(aa, bb)])
    dict[(aa, bb)] = 0

ans = 1
for (x, y) in bad:
    ans = ans * (pow(2, x, p) + pow(2, y, p) - 1) % p
for z in others:
    ans = ans * pow(2, z, p) % p
ans = (ans - 1) % p
if zeros > 0:
    ans = (ans + zeros) % p
print(ans)

Buggy submission (verdict: Runtime Error):
# RE
import math
n = int(input())
a = [0] * n;  b = [0] * n;
for i in range(n):
    a[i], b[i] = [int(x) for x in input().split()]
p = 10 ** 9 + 7

# d = [0] * n
dict = {}
for i in range(n):
    d = math.gcd(a[i], b[i])
    aa = a[i] // d
    bb = b[i] // d
    if aa < 0: # 非負数にする
        aa = -aa
        bb = -bb
    if (aa, bb) in dict:
        dict[(aa, bb)] += 1
    else:
        dict[(aa, bb)] = 1

bad = []
others = []
while dict:
    aa, bb = list(dict.keys())[0]
    if bb >= 0 and (bb, -aa) in dict:
        bad.append((dict[(aa, bb)], dict[(bb, -aa)]))
        del dict[(bb, -aa)]
    elif (-bb, aa) in dict:
        bad.append((dict[(aa, bb)], dict[(-bb, aa)]))
        del dict[(-bb, aa)]
    else:
        others.append(dict[(aa, bb)])
    del dict[(aa, bb)]

for (aa, bb) in dict:
    if dict[(aa, bb)] == 0: continue
    if (bb, -aa) in dict:
        bad.append((dict[(aa, bb)], dict[(bb, -aa)]))
        dict[(bb, -aa)] = 0
    elif (-bb, aa) in dict:
        bad.append((dict[(aa, bb)], dict[(-bb, aa)]))
        dict[(-bb, aa)] = 0
    else:
        others.append(dict[(aa, bb)])
    dict[(aa, bb)] = 0

ans = 1
for (x, y) in bad:
    ans = ans * (pow(2, x, p) + pow(2, y, p) - 1) % p
for z in others:
    ans = ans * pow(2, z, p) % p
ans -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
import math
n = int(input())
a = [0] * n;  b = [0] * n;
for i in range(n):
    a[i], b[i] = [int(x) for x in input().split()]
p = 10 ** 9 + 7

d = [0] * n
dict = {}
for i in range(n):
    d[i] = math.gcd(a[i], b[i])
    aa = a[i] // d[i]
    bb = b[i] // d[i]
    if aa < 0: # 非負数にする
        aa = -aa
        bb = -bb
    if (aa, bb) in dict:
        dict[(aa, bb)] += 1
    else:
        dict[(aa, bb)] = 1

bad = []
others = []
while dict:
    aa, bb = list(dict.keys())[0]
    if bb >= 0 and (bb, -aa) in dict:
        bad.append((dict[(aa, bb)], dict[(bb, -aa)]))
        del dict[(bb, -aa)]
    elif (-bb, aa) in dict:
        bad.append((dict[(aa, bb)], dict[(-bb, aa)]))
        del dict[(-bb, aa)]
    else:
        others.append(dict[(aa, bb)])
    del dict[(aa, bb)]

ans = 1
for (x, y) in bad:
    ans = ans * (pow(2, x, p) + pow(2, y, p) - 1) % p
for z in others:
    ans = ans * pow(2, z, p) % p
ans -= 1
print(ans)