Problem p02679 (python)

Fixed submission:
import math

mod = 1000000007
n = int(input())
zero = 0
dic = {}
dic[(0, 1.5)] = 0
dic[(1.5, 0)] = 0
for i in range(n):
    ai, bi = map(int, input().split())
    idx = (ai, bi)
    if ai < 0:
        ai, bi = -ai, -bi
    if idx == (0, 0):
        zero += 1
    elif idx[0] == 0:
        dic[(0, 1.5)] += 1
    elif idx[1] == 0:
        dic[(1.5, 0)] += 1
    else:
        g = math.gcd(abs(ai), abs(bi))
        ai = ai // g
        bi = bi // g
        idx = (ai, bi)
        if idx in dic:
            dic[idx] += 1
        else:
            dic[idx] = 1

ans = 1
already = set([])
for key, value in dic.items():
    if key in already: continue
    a, b = key
    if a == 1.5 or b == 1.5:
        pair1 = dic[(0, 1.5)]
        pair2 = dic[(1.5, 0)]
        ans = (ans * (pow(2, pair1, mod)+pow(2, pair2, mod)-1)) % mod
        already.add((0, 1.5))
        already.add((1.5, 0))
    else:
        if b > 0: idx = (b, -a)
        else: idx = (-b, a)
        if idx in dic:
            pair1 = dic[key]
            pair2 = dic[idx]
            ans = (ans * (pow(2, pair1, mod)+pow(2, pair2, mod)-1)) % mod
            already.add(key)
            already.add(idx)
        else:
            pair1 = dic[key]
            ans = (ans * pow(2, pair1, mod)) % mod
            already.add(key)

ans = (ans + zero - 1) % mod
print(ans)

Buggy submission (verdict: Runtime Error):
import math

mod = 1000000007
n = int(input())
zero = 0
dic = {}
dic[(0, 1.5)] = 0
dic[(1.5, 0)] = 0
for i in range(n):
    ai, bi = map(int, input().split())
    if ai < 0:
        ai, bi = -ai, -bi
    g = math.gcd(abs(ai), abs(bi))
    ai = ai // g
    bi = bi // g
    idx = (ai, bi)
    if idx == (0, 0):
        zero += 1
    elif idx[0] == 0:
        dic[(0, 1.5)] += 1
    elif idx[1] == 0:
        dic[(1.5, 0)] += 1
    else:
        if idx in dic:
            dic[idx] += 1
        else:
            dic[idx] = 1

ans = 1
already = set([])
for key, value in dic.items():
    if key in already: continue
    a, b = key
    if a == 1.5 or b == 1.5:
        pair1 = dic[(0, 1.5)]
        pair2 = dic[(1.5, 0)]
        ans = (ans * (pow(2, pair1, mod)+pow(2, pair2, mod)-1)) % mod
        already.add((0, 1.5))
        already.add((1.5, 0))
    else:
        if b > 0: idx = (b, -a)
        else: idx = (-b, a)
        if idx in dic:
            pair1 = dic[key]
            pair2 = dic[idx]
            ans = (ans * (pow(2, pair1, mod)+pow(2, pair2, mod)-1)) % mod
            already.add(key)
            already.add(idx)
        else:
            pair1 = dic[key]
            ans = (ans * pow(2, pair1, mod)) % mod
            already.add(key)

ans = ans + zero - 1
print(ans)

Buggy submission (verdict: Runtime Error):
import math

mod = 1000000007
n = int(input())
a, b = [], []
for i in range(n):
    ai, bi = map(int, input().split())
    a.append(ai)
    b.append(bi)

dic = {}
for i in range(n):
    ai, bi = a[i], b[i]
    if ai < 0:
        ai, bi = -ai, -bi
    gcd = math.gcd(abs(ai), abs(bi))
    a[i] = ai//gcd
    b[i] = bi//gcd

    astr = str(a[i])
    bstr = str(b[i])
    idx = bstr + '_' + astr
    if idx in dic:
        dic[idx] += 1
    else:
        dic[idx] = 1

ans = 1
for value in dic.values():
    ans = (ans * pow(2, value, mod)) % mod
all = ans
ans -= 1

dame = 1
all_kousin = all
for key in dic.keys():
    if key[0] != '-':
        mae, usiro = key.split('_')
        idx = '-' + usiro + '_' + mae
        if idx in dic:
            n = dic[key]
            m = dic[idx]
            dame = (dame * (pow(2, n, mod)*pow(2, m, mod)-n-m-1)) % mod
            all_kousin = all_kousin * pow(pow(2, n, mod), mod-2, mod) * pow(pow(2, m, mod), mod-2, mod) % mod
ans = (ans - dame*all_kousin) % mod
print(ans)