Problem p02679 (python)

Fixed submission:
import sys
from collections import defaultdict
from queue import deque
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def egcd(a: int, b: int):
    """
    A solution of ax+by=gcd(a,b). Returns
    (gcd(a,b), (x, y)).
    """
    if a == 0:
        return b, 0, 1
    else:
        g, x, y = egcd(b % a, a)
        return g, y - (b // a) * x, x


def modinv(a: int, mod: int = 10**9 + 7):
    """
    Returns a^{-1} modulo m.
    """
    g, x, y = egcd(a, mod)
    if g > 1:
        raise Exception('{}^(-1) mod {} does not exist.'.format(a, mod))
    else:
        return x % mod


def modpow(a: int, n: int, mod: int = 10**9 + 7):
    """
    Returns a^n mod m. (-inf < n < inf)
    """
    if a in [-1, 0, 1]:
        if a == -1:
            return 1 if n & 1 == 0 else mod - 1
        else:
            return a

    x, k = (a % mod, n) if n >= 0 else (modinv(a, mod), -n)

    ret = 1
    while k > 0:
        if k & 1 == 1:
            ret = ret * x % mod
        x = (x**2) % mod
        k = k >> 1

    return ret


def main():
    N = gete(int)

    d = dict()

    for _ in range(N):
        a, b = geta(int)
        if a == b == 0:
            if (0, 0) in d:
                d[(0, 0)] += 1
            else:
                d[(0, 0)] = 1
            continue

        if a != 0 and b != 0:
            g, _, _ = egcd(a, b)
            ai, bi = a // g, b // g
            if ai < 0:
                ai *= -1
                bi *= -1

            aj, bj = bi, -ai
            if aj < 0:
                aj *= -1
                bj *= -1
        elif a == 0:
            ai, bi = 0, 1
            aj, bj = 1, 0
        elif b == 0:
            ai, bi = 1, 0
            aj, bj = 0, 1

        if (ai, bi) in d:
            d[(ai, bi)][0] += 1
        elif (aj, bj) in d:
            d[(aj, bj)][1] += 1
        else:
            d[(ai, bi)] = [1, 0]

    mod = 1000000007
    ans = 1
    for k in d:
        if k != (0, 0):
            n = modpow(2, d[k][0], mod)
            m = modpow(2, d[k][1], mod)

            ans = ans * (n + m - 1) % mod
    if (0, 0) in d:
        ans = (ans + d[(0, 0)]) % mod

    print((ans - 1) % mod)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
from queue import deque
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def egcd(a: int, b: int):
    """
    A solution of ax+by=gcd(a,b). Returns
    (gcd(a,b), (x, y)).
    """
    if a == 0:
        return b, 0, 1
    else:
        g, x, y = egcd(b % a, a)
        return g, y - (b // a) * x, x


def modinv(a: int, mod: int = 10**9 + 7):
    """
    Returns a^{-1} modulo m.
    """
    g, x, y = egcd(a, mod)
    if g > 1:
        raise Exception('{}^(-1) mod {} does not exist.'.format(a, mod))
    else:
        return x % mod


def modpow(a: int, n: int, mod: int = 10**9 + 7):
    """
    Returns a^n mod m. (-inf < n < inf)
    """
    if a in [-1, 0, 1]:
        if a == -1:
            return 1 if n & 1 == 0 else mod - 1
        else:
            return a

    x, k = (a % mod, n) if n >= 0 else (modinv(a, mod), -n)

    ret = 1
    while k > 0:
        if k & 1 == 1:
            ret = ret * x % mod
        x = (x**2) % mod
        k = k >> 1

    return ret


def main():
    N = gete(int)

    d = dict()

    for _ in range(N):
        a, b = geta(int)
        if a == b == 0:
            if (0, 0) in d:
                d[(0, 0)][0] += 1
            else:
                d[(0, 0)] = [1, 0]
            continue

        if a != 0 and b != 0:
            g, _, _ = egcd(a, b)
            ai, bi = a // g, b // g
            if ai < 0:
                ai *= -1
                bi *= -1

            aj, bj = bi, -ai
            if aj < 0:
                aj *= -1
                bj *= -1
        elif a == 0:
            ai, bi = 0, 1
            aj, bj = 1, 0
        elif b == 0:
            ai, bi = 1, 0
            aj, bj = 0, 1

        if (ai, bi) in d:
            d[(ai, bi)][0] += 1
        elif (aj, bj) in d:
            d[(aj, bj)][1] += 1
        else:
            d[(ai, bi)] = [1, 0]

    mod = 1000000007
    ans = 1
    for k in d:
        if k != (0, 0):
            n = modpow(2, d[k][0], mod)
            m = modpow(2, d[k][1], mod)

            ans = ans * (n + m - 1) % mod
    ans -= 1
    if (0, 0) in d:
        dd = d[(0, 0)]
        ans += modpow(2, dd, mod) - 1

    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
from queue import deque
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def egcd(a: int, b: int):
    """
    A solution of ax+by=gcd(a,b). Returns
    (gcd(a,b), (x, y)).
    """
    if a == 0:
        return b, 0, 1
    else:
        g, x, y = egcd(b % a, a)
        return g, y - (b // a) * x, x


def modinv(a: int, mod: int = 10**9 + 7):
    """
    Returns a^{-1} modulo m.
    """
    g, x, y = egcd(a, mod)
    if g > 1:
        raise Exception('{}^(-1) mod {} does not exist.'.format(a, mod))
    else:
        return x % mod


def modpow(a: int, n: int, mod: int = 10**9 + 7):
    """
    Returns a^n mod m. (-inf < n < inf)
    """
    if a in [-1, 0, 1]:
        if a == -1:
            return 1 if n & 1 == 0 else mod - 1
        else:
            return a

    x, k = (a % mod, n) if n >= 0 else (modinv(a, mod), -n)

    ret = 1
    while k > 0:
        if k & 1 == 1:
            ret = ret * x % mod
        x = (x**2) % mod
        k = k >> 1

    return ret


def main():
    N = gete(int)

    d = dict()

    for _ in range(N):
        a, b = geta(int)
        g, _, _ = egcd(a, b)
        ai, bi = a // g, b // g
        if ai < 0:
            ai *= -1
            bi *= -1

        aj, bj = bi, -ai
        if aj < 0:
            aj *= -1
            bj *= -1

        if (ai, bi) in d:
            d[(ai, bi)][0] += 1
        elif (aj, bj) in d:
            d[(aj, bj)][1] += 1
        else:
            d[(ai, bi)] = [1, 0]
    mod = 1000000007
    ans = 1
    for k in d:
        n = modpow(2, d[k][0], mod)
        m = modpow(2, d[k][1], mod)

        ans = ans * (n + m - 1) % mod
    print(ans - 1)


if __name__ == "__main__":
    main()