Problem:
 Score: 500 points    Problem Statement  We have caught N sardines. The deliciousness and fragrantness of the i -th sardine is A_i and B_i , respectively.  We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.  The i -th and j -th sardines (i \neq j) are on bad terms if and only if A_i \cdot A_j + B_i \cdot B_j = 0 .  In how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007 .      Constraints   All values in input are integers.  1 \leq N \leq 2 \times 10^5  -10^{18} \leq A_i, B_i \leq 10^{18}         Input  Input is given from Standard Input in the following format:  N  A_1  B_1  :  A_N  B_N       Output  Print the count modulo 1000000007 .        Sample Input 1 3
1 2
-1 1
2 -1      Sample Output 1 5  There are five ways to choose the set of sardines, as follows:   The 1 -st  The 1 -st and 2 -nd  The 2 -nd  The 2 -nd and 3 -rd  The 3 -rd        Sample Input 2 10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4      Sample Output 2 479

Solution (Python):
from collections import defaultdict 
import sys
input = sys.stdin.readline
from math import gcd

MOD = 1000000007

dic1 = defaultdict(int)

All_zero = 0

S = set()
N = int(input())
ans = 0 
for i in range(N):
    a, b = map(int, input().split())
    if a == 0 and b == 0: 
        All_zero += 1
    elif a == 0:
        S.add((0, 1))
        dic1[(0, 1)] += 1
    elif b == 0:
        S.add((1, 0))
        dic1[(1, 0)] += 1
    else:
        GCD = gcd(a, b)
        a //= GCD
        b //= GCD
        if a < 0: 
            a *= -1
            b *= -1
        S.add((a, b))
        dic1[(a, b)] += 1
        
lst = []
for a, b in S:
    tmp11 = dic1[(a, b)]
    A = a
    B = b
    if B <= 0:
        B *= -1
        A *= -1
    tmp2 = dic1[(B, -A)]
    
    dic1[(B, -A)] = 0
    if tmp11 > 0:
        lst.append((tmp11, tmp2))



ans = 1
for a, b in lst:
    tmp = (pow(2, a, MOD) - 1) + (pow(2, b, MOD) - 1) + 1 
    ans *= tmp
    ans %= MOD

ans = (ans - 1 + All_zero) % MOD
print (ans % MOD)

Solution (Python):
import sys
from collections import defaultdict

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

MOD = 10**9 + 7
P = 2 ** 61 - 1

N = int(readline())
m = map(int, read().split())
A, B = zip(*zip(m, m))

pow2 = [1] * (N + 10)
for n in range(1, N + 10):
    pow2[n] = pow2[n - 1] * 2 % MOD

def inv_mod(a):
    b = P
    u, v = 1, 0
    while a:
        t = b // a
        a, b = b - t * a, a
        u, v = v - t * u, u
    if b < 0:
        v = -v
    return v % P

def to_key(a, b):
    if a == 0:
        return -1
    x = inv_mod(a) * b % P
    return x

def pair_key(key):
    if key == -1:
        return 0
    if key == 0:
        return -1
    return P - inv_mod(key)
  
counter = defaultdict(int)
origin = 0
for a, b in zip(A, B):
    if a == b == 0:
        origin += 1
        continue
    key = to_key(a, b)
    counter[key] += 1

answer = origin
k = 1
for key, cnt in counter.items():
    key1 = pair_key(key)
    if key1 not in counter:
        k *= pow2[cnt]
    elif key < key1:
        x, y = cnt, counter[key1]
        k *= pow2[x] + pow2[y] - 1
    k %= MOD
answer += k - 1
answer %= MOD
print(answer)

Solution (Python):
import sys
from collections import defaultdict
from functools import lru_cache

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

MOD = 10**9 + 7
P = 2 ** 61 - 1

N = int(readline())
m = map(int, read().split())
A, B = zip(*zip(m, m))

pow2 = [1] * (N + 10)
for n in range(1, N + 10):
    pow2[n] = pow2[n - 1] * 2 % MOD

@lru_cache(None)
def inv_mod(a):
    b = P
    u, v = 1, 0
    while a:
        t = b // a
        a, b = b - t * a, a
        u, v = v - t * u, u
    if b < 0:
        v = -v
    return v % P

def to_key(a, b):
    if a == 0:
        return -1
    x = inv_mod(a) * b % P
    return x

def pair_key(key):
    if key == -1:
        return 0
    if key == 0:
        return -1
    return P - inv_mod(key)
  
counter = defaultdict(int)
origin = 0
for a, b in zip(A, B):
    if a == b == 0:
        origin += 1
        continue
    key = to_key(a, b)
    counter[key] += 1

answer = origin
k = 1
for key, cnt in counter.items():
    key1 = pair_key(key)
    if key1 not in counter:
        k *= pow(2, cnt, MOD)
    elif key < key1:
        x, y = cnt, counter[key1]
        k *= pow(2, x, MOD) + pow(2, y, MOD) - 1
    k %= MOD
answer += k - 1
answer %= MOD
print(answer)

Solution (Python):
def gcd(x, y):
    x = abs(x)
    y = abs(y)

    if x < y:
        x, y = y, x

    while y:
        x, y = y, x % y

    return x


N = int(input())
seen = dict()
zeroes = 0
MOD = 10 ** 9 + 7
for _ in range(N):
    a, b = map(int, input().split())
    if a == 0 and b == 0:
        zeroes += 1  
        continue

    if a < 0 or a == 0 and b < 0:
        a, b = -a, -b
    if b <= 0:
        a, b = -b, a
        flip = 1
    else:
        flip = 0
    g = gcd(a, b)
    a //= g
    b //= g  
    if (a, b) not in seen:
        seen[(a, b)] = [0, 0]

    seen[(a, b)][flip] += 1


out = 1
for v in seen:
    a, b = seen[v]  
    out *= (pow(2, a, MOD) + pow(2, b, MOD) - 1)
    out %= MOD
out -= 1  

out += zeroes  
out %= MOD
print(out)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import *
from math import gcd

N = int(input())
cnt = defaultdict(int)
abzero = 0
azero = 0
bzero = 0

for _ in range(N):
    A, B = map(int, input().split())
    
    if A==0 and B==0:
        abzero += 1
        continue
    elif A==0:
        azero += 1
        continue
    elif B==0:
        bzero += 1
        continue
        
    G = gcd(abs(A), abs(B))
    A //= G
    B //= G
    
    if (A<0 and B>0) or (A<0 and B<0):
        A *= -1
        B *= -1
    
    cnt[(A, B)] += 1

ans = 1
MOD = 10**9+7
visited = set()

for k in cnt.keys():
    if k in visited:
        continue
    
    if k[0]>0 and k[1]>0:
        k2 = (k[1], -k[0])
    else:
        k2 = (-k[1], k[0])
    
    if k2 not in cnt:
        ans *= pow(2, cnt[k], MOD)
        ans %= MOD
    else:
        ans *= pow(2, cnt[k], MOD)+pow(2, cnt[k2], MOD)-1
        ans %= MOD
        visited.add(k2)

ans *= pow(2, azero, MOD)+pow(2, bzero, MOD)-1
ans %= MOD
ans += abzero-1
ans %= MOD
print(ans)

Solution (Python):
import math
def modpow(a, n, p):
  if n == 0:
    return 1
  if n == 1:
    return a % p 
  elif n % 2 == 1:
    return a * modpow(a, n-1, p) % p
  else:
    return modpow(a, n//2, p) **2 % p
MOD = 1000000007
n = int(input())

from collections import defaultdict
pp = defaultdict(lambda: 0) 
pm = defaultdict(lambda: 0) 
def seiki(x,y):
    tmp=math.gcd(x,y)
    return (x//tmp,y//tmp)

a0b0 = 0

for i in range(n):
  a, b = map(int, input().split())

  if a == 0 and b == 0:
    a0b0 += 1
  elif a == 0:
    pp[(0,1)] += 1

  elif b == 0:
    pm[(0,1)] += 1

  elif a*b>0:
    tmp = seiki(abs(a),abs(b))
    pp[tmp] += 1

  else:
    tmp = seiki(abs(b),abs(a))
    pm[tmp] += 1

ans = 1
for key in set(pp.keys()) | set(pm.keys()):
    pp_v=pp[key]
    pm_v=pm[key]
    ans*=(modpow(2,pm_v,MOD)+modpow(2,pp_v,MOD)-1)
    ans%=MOD

ans = (ans+a0b0-1) % MOD
print(ans)

Solution (Python):
import sys
from math import gcd


from fractions import Fraction
import itertools

input=sys.stdin.readline
def main():
  ans=1
  mod=1000000007
  n0=0
  na=nb=0
  n=int(input())
  d={}

  for i in range(n):
      a,b=map(int,input().split())
      if b==0 and a==0:
          n0+=1
      elif b==0:
          nb+=1
      elif a==0:
          na+=1
      else:
          g=gcd(a,b)
          if b<0:
            a*=-1
            b*=-1
          s=(a//g,b//g)
          if s not in d:
              d[s]=1
          else:
              d[s]+=1
  for k,v in d.items():
      a,b=k
      if a>0:
        if (-b,a) in d:
            if d[(a,b)]!=-1:
                ans*=pow(2,d[(-b, a)])-1+pow(2,d[(a,b)])-1+1
                ans=ans%mod
                d[(-b,a)]=-1
        else:
            ans*=pow(2,d[(a,b)])
            ans=ans%mod
            
      else:
        if (b,-a) in d:
            if d[(a,b)]!=-1:
                ans*=pow(2,d[(b, -a)])-1+pow(2,d[(a,b)])-1+1
                ans=ans%mod
                d[(b,-a)]=-1
        else:
            ans*=pow(2,d[(a,b)])
            ans=ans%mod
  ans*=pow(2,na)-1+pow(2,nb)-1+1
  print((ans-1+n0)%mod)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
from math import gcd
def solve():
    N = int(input())
    AB = [tuple(map(int,input().split())) for i in range(N)]
    MOD = 10**9+7

    from collections import Counter
    ctr = Counter()
    az = bz = zz = 0
    for a,b in AB:
        if a==b==0:
            zz += 1
        elif a==0:
            az += 1
        elif b==0:
            bz += 1
        else:
            g = gcd(a,b)
            a,b = a//g, b//g
            if b < 0:
                b *= -1
                a *= -1
            ctr[(a,b)] += 1
    ans = pow(2,az,MOD) + pow(2,bz,MOD) - 1

    used = set()
    for (a1,b1),v in ctr.items():
        if (a1,b1) in used: continue
        a2,b2 = -b1,a1
        if b2 < 0:
            b2 *= -1
            a2 *= -1
        if (a2,b2) in ctr:
            used.add((a2,b2))
        r = pow(2,v,MOD) + pow(2,ctr[(a2,b2)],MOD) - 1
        ans *= r
        ans %= MOD
    ans += zz - 1
    ans %= MOD
    print(ans)
solve()

Solution (Python):
import sys
input = sys.stdin.readline
from collections import *
from math import gcd

N = int(input())
cnt = defaultdict(int)
abzero = 0
azero = 0
bzero = 0

for _ in range(N):
    A, B = map(int, input().split())
    
    if A==0 and B==0:
        abzero += 1
        continue
    elif A==0:
        azero += 1
        continue
    elif B==0:
        bzero += 1
        continue
        
    G = gcd(abs(A), abs(B))
    A //= G
    B //= G
    
    if (A<0 and B>0) or (A<0 and B<0):
        A *= -1
        B *= -1
    
    cnt[(A, B)] += 1

ans = 1
MOD = 10**9+7
visited = set()

for k in cnt.keys():
    if k in visited:
        continue
    
    if k[0]>0 and k[1]>0:
        k2 = (k[1], -k[0])
    else:
        k2 = (-k[1], k[0])
    
    if k2 not in cnt:
        ans *= pow(2, cnt[k], MOD)
    else:
        ans *= pow(2, cnt[k], MOD)+pow(2, cnt[k2], MOD)-1
        visited.add(k2)

ans *= pow(2, azero, MOD)+pow(2, bzero, MOD)-1
ans %= MOD
ans += abzero-1
ans %= MOD
print(ans)

Solution (Python):
import math


def main():

    mod = 1000000007
    N = int(input())

    A = [0]
    B = [0]

    for n in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)

    calc = dict()
    non_count = 0

    for i in range(1, N + 1):

        if A[i] == 0 and B[i] == 0:
            non_count += 1
        elif A[i] == 0:
            k = (0, 1)
            if calc.get(k):
                calc[k][0] += 1
            else:
                calc[k] = [1, 0]
        elif B[i] == 0:
            k = (0, 1)
            if calc.get(k):
                calc[k][1] += 1
            else:
                calc[k] = [0, 1]
        else:
            r = math.gcd(A[i], B[i])
            m = 1
            if A[i] * B[i] < 0:
                m = -1
            k1 = (m * abs(A[i] // r), abs(B[i] // r))
            k2 = (-m * abs(B[i] // r), abs(A[i] // r))
            if calc.get(k1):
                calc[k1][0] += 1
            elif not calc.get(k2):
                calc[k1] = [1, 0]
            else:
                calc[k2][1] += 1

    N = N - non_count

    count = 0
    for k, v in calc.items():
        if not v[0] * v[1] == 0:
            count += v[0] + v[1]

    ans = pow(2, N - count, mod)
    for k, v in calc.items():
        if not v[0] * v[1] == 0:
            comb = (pow(2, v[0]) + pow(2, v[1]) - 1)
            ans = (ans * comb) % mod

    print((ans - 1 + non_count) % mod)


if __name__ == '__main__':
    main()

Solution (Python):
import math


def main():

    mod = 1000000007
    N = int(input())

    A = [0]
    B = [0]

    for n in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)

    calc = dict()
    non_count = 0

    for i in range(1, N + 1):

        if A[i] == 0 and B[i] == 0:
            non_count += 1
        elif A[i] == 0:
            k = (0, 1)
            if calc.get(k):
                calc[k][0] += 1
            else:
                calc[k] = [1, 0]
        elif B[i] == 0:
            k = (0, 1)
            if calc.get(k):
                calc[k][1] += 1
            else:
                calc[k] = [0, 1]
        else:
            r = math.gcd(A[i], B[i])
            m = 1
            if A[i] * B[i] < 0:
                m = -1
            k1 = (m * abs(A[i] // r), abs(B[i] // r))
            k2 = (-m * abs(B[i] // r), abs(A[i] // r))
            if calc.get(k1):
                calc[k1][0] += 1
            elif not calc.get(k2):
                calc[k1] = [1, 0]
            else:
                calc[k2][1] += 1

    N = N - non_count

    count = 0
    for k, v in calc.items():
        if not v[0] * v[1] == 0:
            count += v[0] + v[1]

    ans = pow(2, N - count, mod)
    for k, v in calc.items():
        if not v[0] * v[1] == 0:
            comb = (pow(2, v[0]) + pow(2, v[1]) - 1)
            ans = (ans * comb) % mod

    print((ans - 1 + non_count) % mod)


if __name__ == '__main__':
    main()

Solution (Python):
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)

Solution (Python):









from math import gcd
from collections import defaultdict
import sys

input = sys.stdin.buffer.readline

mod = 10 ** 9 + 7
n = int(input())
cnt = 0
cnt += n
cnt10 = 0
cnt01 = 0
zerocnt = 0
kumi = defaultdict(int)
for i in range(n):
    a, b = map(int, input().split())
    if a == 0 and b == 0:
        zerocnt += 1
        continue
    elif b == 0:
        cnt10 += 1
        continue
    elif a == 0:
        cnt01 += 1
        continue
    g = gcd(a, b)
    a //= g
    b //= g
    if a < 0:
        a *= -1
        b *= -1
    kumi[(a, b)] += 1
    kumi[(b, -a)] += 0
ans = 1
cnt -= zerocnt
ans *= pow(2, cnt10 + cnt01, mod) - (pow(2, cnt10, mod) - 1) * (pow(2, cnt01, mod) - 1)
ans %= mod
cnt -= cnt10 + cnt01
for a, b in kumi.keys():
    if b <= 0:
        continue
    s = kumi[(a, b)]
    t = kumi[(b, -a)]
    ans *= pow(2, (s + t), mod) - (pow(2, s, mod) - 1) * (pow(2, t, mod) - 1)
    ans %= mod
    cnt -= s + t
if cnt > 0:
    ans *= pow(2, cnt, mod)
    ans %= mod
print((ans - 1 + zerocnt) % mod)

Solution (Python):
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)

Solution (Python):

import sys,pprint, math
from fractions import Fraction
from collections import defaultdict

MOD = 1000000007
N = int(input())
params =[]
hatemasks = []
tilts = defaultdict(lambda: [0,0])
zerozero = 0
zerodiv = 0
for i in range(N):
    (ai, bi) = map(int,input().split())
    if ai == 0 and bi == 0:
        zerozero += 1
        continue
    if (ai < 0) or (ai == 0 and bi < 0):
        ai *= -1
        bi *= -1
    g = math.gcd(ai,bi)
    ai = ai // g
    bi = bi // g
    if bi > 0:
        tilts[(ai,bi)][0] += 1
    else:
        tilts[(-bi,ai)][1] += 1
print(tilts,file=sys.stderr)


ans = 1
for(v1,v2) in tilts.values():
    ans *= 1 +pow(2,v1,MOD)-1 +pow(2,v2,MOD)-1
    ans %= MOD
ans = (ans+zerozero-1) % MOD
print(ans)

Solution (Python):
import sys
from collections import defaultdict

sys.setrecursionlimit(500000)

class Fraction:
    def gcd(self, a ,b):
        a,b = max(a,b),min(a,b)
        while a%b!=0:
            a,b = b,a%b
        return b

    def frac(self,a,b):
        if a==0 and b==0:
            return (0,0)
        elif a==0:
            return (0,1)
        elif b==0:
            return (1,0)
        else:
            d = self.gcd(abs(a),abs(b))
            if a<0:
                a = -a
                b = -b
            return (a//d,b//d)
    
    def __init__(self, a, b):
        self.value = self.frac(a,b)

    def v(self):
        return self.value

    def __lt__(self, other):
        assert self.value!=(0,0) and other.value!=(0,0), 'value (0,0) cannot be compared.'
        a,b = self.value
        c,d = other.value
        return a*d < b*c
    
    def __le__(self, other):
        assert self.value!=(0,0) and other.value!=(0,0), 'value (0,0) cannot be compared.'
        a,b = self.value
        c,d = other.value
        return a*d <= b*c

    def __eq__(self, other):
        a,b = self.value
        c,d = other.value
        return a==c and b==d

    def __ne__(self, other):
        a,b = self.value
        c,d = other.value
        return not (a==c and b==d)

    def __gt__(self, other):
        assert self.value!=(0,0) and other.value!=(0,0), 'value (0,0) cannot be compared.'
        a,b = self.value
        c,d = other.value
        return a*d > b*c

    def __ge__(self, other):
        assert self.value!=(0,0) and other.value!=(0,0), 'value (0,0) cannot be compared.'
        a,b = self.value
        c,d = other.value
        return a*d >= b*c

    def __add__(self, other):
        a,b = self.value
        c,d = other.value
        return Fraction(a*d + b*c, b*d)
        
    def __sub__(self, other):
        a,b = self.value
        c,d = other.value
        return Fraction(a*d - b*c, b*d)

    def __mul__(self, other):
        a,b = self.value
        c,d = other.value
        return Fraction(a*c, b*d)

    def __truediv__(self, other):
        a,b = self.value
        c,d = other.value
        return Fraction(a*d, b*c)
    
    def __neg__(self):
        a,b = self.value
        return Fraction(-a,b)
        
    def inv(self):
        return Fraction(1,1) / self

def input():
    return sys.stdin.readline()[:-1]

def main():
    N = int(input())

    vec = [list(map(int,input().split())) for i in range(N)]

    arg = defaultdict(int)
    zero = 0
    for v in vec:
        if v[0] == 0 and v[1] == 0:
            zero += 1
        else:
            f = Fraction(v[0],v[1])
            arg[f.v()] += 1
            arg[(-f.inv()).v()] += 0

    pair = []
    fd = lambda : False

    checked_list = defaultdict(fd)
    for key in arg:
        if not checked_list[key]:
            inv = (-Fraction(*key).inv()).v()
            pair.append([arg[key],arg[inv]])
            checked_list[inv] = True

    mod = 1000000007

    ans = 1

    for p1,p2 in pair:
        ans *= pow(2,p1,mod) + pow(2,p2,mod) - 1
        ans %= mod

    ans += zero
    ans %= mod

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

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
AB = [list(map(int,input().split())) for _ in range (N)]
MOD = 10**9+7

from collections import defaultdict
from math import gcd
def cmp(x,y):
    g = gcd(abs(x),abs(y))
    if x*y<0:
        return (-abs(x)//g, abs(y)//g)
    else:
        return (abs(x)//g, abs(y)//g)
def rev(x):
    
    
    a = x[0]
    b = x[1]
    if a<0:
        return (abs(b), abs(a))
    else:
        return (-abs(b), abs(a))

dnums = defaultdict(int)
dnums2 = defaultdict(int)
num_zero = 0
num_az = 0
num_bz = 0
keys = set()
for a,b in AB:
    if a!=0 and b!=0:
        k1 = cmp(a,b)
        dnums[k1] += 1
        keys.add(k1)
    elif a==0 and b==0:
        num_zero += 1
    elif a == 0 and b!=0:
        num_az += 1
    elif a != 0 and b==0:
        num_bz += 1

mydict = dict()
for key in list(keys):
    mydict[key] = False

ans = 1
for key in list(keys):
    if mydict[key]:
        continue
    rkey = rev(key)
    if rkey in keys:
        mydict[rev(key)] = True
        mydict[key] = True

    ans *= pow(2, dnums[key], MOD) + pow(2, dnums[rkey], MOD) - 1
    ans %= MOD

ans *= pow(2, num_az, MOD) + pow(2, num_bz, MOD) - 1
ans += num_zero
ans += MOD - 1
print(ans%MOD)

Solution (Python):
import sys,math
input = sys.stdin.buffer.readline

mod = 1000000007
n = int(input())

ab = []
ang = dict()
rev = dict()
dg = 10**20
zero = 0

for i in range(n):
    a,b = map(int,input().split())
    if a == 0 and b == 0:
        zero += 1
    elif a == 0:
        if 1 in ang:
            ang[1] += 1
        else:
            ang[1] = 1
    elif b == 0:
        if 1 in rev:
            rev[1] += 1
        else:
            rev[1] = 1
    elif a*b > 0:
        if a < 0 and b < 0:
            a,b = -a,-b
        g = math.gcd(a,b)
        a,b = a//g,b//g

        if a*dg + b in ang:
            ang[a*dg + b] += 1
        else:
            ang[a*dg + b] = 1
    else:
        a,b = abs(a),abs(b)
        g = math.gcd(a,b)
        a,b = a//g,b//g

        if b*dg + a in rev:
            rev[b*dg + a] += 1
        else:
            rev[b*dg + a] = 1

rest = n - zero

res = 1

for e in ang:
    if e in rev:
        res = res*(pow(2,ang[e],mod) + pow(2,rev[e],mod) - 1)%mod
        rest -= ang[e] + rev[e]

res = res*pow(2,rest,mod)%mod - 1 + zero

print(res%mod)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")











N = int(input())
MOD = 10**9+7
AB = []
x = 0
y = 0
d = defaultdict(int)
ch = defaultdict(int)
flag = 0
for _ in range(N):
    a,b = map(int,input().split())
    if a == 0 and b == 0:
        flag += 1
    elif a == 0:
        d[(0,1)] += 1
        AB.append([0,1])
    elif b == 0:
        d[(1,0)] += 1
        AB.append([1,0])
    else:
        dd = math.gcd(abs(a),abs(b))
        a //= dd
        b //= dd
        if a < 0:
            a *= -1
            b *= -1
        d[(a,b)] += 1
        AB.append([a,b])
n = len(AB)
ans = 1
if n > 0:
  for i in range(n):
      a,b = AB[i]
      if ch[(a,b)] == 1:
          continue
      m = d[(a,b)]
      if b > 0:
          l = d[(b,(-1)*a)]
          ch[(a,b)] = 1
          ch[(b,(-1)*a)] = 1
      elif a == 0 or b == 0:
          l = d[(b,a)]
          ch[(a,b)] = 1
          ch[(b,a)] = 1
      else:
          l = d[((-1)*b,a)]
          ch[(a,b)] = 1
          ch[((-1)*b,a)] = 1
      ans *= (pow(2,m,MOD)+pow(2,l,MOD)-1)
      ans = ans % MOD
ans = ans % MOD
ans = ans -1 + flag
ans = ans % MOD
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")











N = int(input())
MOD = 10**9+7
AB = []
x = 0
y = 0
d = defaultdict(int)
ch = defaultdict(int)
flag = 0
for _ in range(N):
    a,b = map(int,input().split())
    if a == 0 and b == 0:
        flag += 1
        continue
    elif a == 0:
        x += 1
        continue
    elif b == 0:
        y += 1
        continue
    else:
        dd = math.gcd(abs(a),abs(b))
        if dd < 0:
          dd *= -1
        a //= dd
        b //= dd
        if a < 0:
            a *= -1
            b *= -1
        d[(a,b)] += 1
        AB.append([a,b])
n = len(AB)
ans = 1
if n > 0:
  for i in range(n):
      a,b = AB[i]
      if ch[(a,b)] == 1:
          continue
      m = d[(a,b)]
      if b > 0:
          l = d[(b,(-1)*a)]
          ch[(a,b)] = 1
          ch[(b,(-1)*a)] = 1
      else:
          l = d[((-1)*b,a)]
          ch[(a,b)] = 1
          ch[((-1)*b,a)] = 1
      ans *= (pow(2,m,MOD)+pow(2,l,MOD)-1+MOD)
      ans = ans % MOD
ans *= (pow(2,x,MOD)+pow(2,y,MOD)-1+MOD)
ans = ans % MOD
ans = ans -1 + flag
ans = ans % MOD
print(ans)

Solution (Python):
from math import gcd

N = int(input())
AB = [map(int, input().split()) for _ in range(N)]

t = set()
d = {}
d[0] = {}
d[0][0] = 0
for a, b in AB:
    i = gcd(a, b)
    if i != 0:
        a //= i
        b //= i
    t.add((a, b))
    d.setdefault(a, {})
    d[a].setdefault(b, 0)
    d[a][b] += 1
if (0, 0) in t:
    t.remove((0, 0))

used = set()
result = 1
for a, b in t:
    if (a, b) in used:
        continue
    used.add((a, b))
    i = d[a][b]
    j, k, l = 0, 0, 0
    if -a in d and -b in d[-a]:
        j = d[-a][-b]
        used.add((-a, -b))
    if -b in d and a in d[-b]:
        k = d[-b][a]
        used.add((-b, a))
    if b in d and -a in d[b]:
        l = d[b][-a]
        used.add((b, -a))
    result *= pow(2, i + j, 1000000007) + pow(2, k + l, 1000000007) - 1
    result %= 1000000007
result += d[0][0] - 1
result %= 1000000007
print(result)

Solution (Python):
from math import gcd
from collections import defaultdict

n = int(input())
AB = [list(map(int, input().split())) for _ in range(n)]

MOD = 1000000007

box = defaultdict(int)
zeros = 0
for a, b in AB:
  if a == b == 0:
    zeros += 1
  else:
    if a != 0 and b == 0:
      box[(-1, 1, 0)] += 1
    elif a == 0 and b != 0:
      box[(1, 0, 1)] += 1
    else:
      g = gcd(a, b)
      ga = a // g
      gb = b // g
      if (a // b) < 0:
        s = -1
      else:
        s = 1
      box[(s, abs(ga), abs(gb))] += 1
   
nakawaru = dict()
others = 0

for (s, i, j), v in box.items():
    if (-s, j, i) in box.keys():
        if (s, i, j) not in nakawaru.keys() and (-s, j, i) not in nakawaru.keys():    
            nakawaru[(s, i, j)] = (box[(s, i, j)], box[(-s, j, i)])
    else:
        others += v

seed = 1
for i, j in nakawaru.values():
    seed *= (pow(2, i, MOD) + pow(2, j, MOD) - 1)
    seed %= MOD

ans = zeros + pow(2, others, MOD) * seed - 1
ans %= MOD

print(ans)

Solution (Python):
from collections import defaultdict
from math import gcd

n = int(input())

zeros = 0
d = defaultdict(lambda: [0, 0])
for _ in range(n):
    a, b = map(int, input().split())
    if a == 0 and b == 0:
        zeros += 1
        continue
    g = gcd(a, b)
    a, b = a // g, b // g
    if a == 0:
        b = 1
    if b == 0:
        a = 1
    if b < 0:
        a, b = -a, -b
    if a <= 0:
        d[(b, -a)][1] += 1
    else:
        d[(a, b)][0] += 1


mod = 1000000007
ans = 1
for v in d.values():
    ans = ans * (pow(2, v[0], mod) + pow(2, v[1], mod) - 1) % mod

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

Solution (Python):
from collections import defaultdict
from math import gcd

def main():
    N = int(input())
    MOD = 10 ** 9 + 7
    zero = 0
    d = defaultdict(lambda: defaultdict(int))
    for i in range(N):
        A, B = map(int, input().split())
        if A == 0 and B == 0:
            zero += 1
            continue
        if B < 0:
            A = -A
            B = -B
        flag = False
        if A <= 0:
            A, B = B, -A
            flag = True
        g = gcd(A,B)
        A //= g
        B //= g
        if flag:
            d[(A,B)]['first'] += 1
        else:
            d[(A,B)]['second'] += 1
    ans = 1
    for i, j in d.items():
        now = 1
        now += pow(2,j['first'],MOD) - 1
        now += pow(2,j['second'],MOD) - 1
        ans *= now
        ans %= MOD
    ans -= 1
    ans += zero
    ans %= MOD
    print(ans)




    

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
mod = 10**9 + 7
n = int(input())
ab = [[int(x) for x in input().split()] for _ in range(n)]
from collections import defaultdict, Counter
import math

double_zero_cnt = 0 
a_b = [] 
for a, b in ab:
    if a == 0 and b == 0: 
        double_zero_cnt += 1
        continue
    g = math.gcd(a, b)
    a //= g
    b //= g
    sign = 1 if a*b > 0 else -1 if a*b < 0 else 0
    a_b.append((abs(a), abs(b), sign))

c = Counter(a_b) 
key_list = list(c.keys())

d = defaultdict(list) 
for key in key_list:
    a, b, sign = key
    if d[(b, a, -sign)]: 
        continue 
    if c[(b, a, -sign)] != 0:
        d[(a, b, sign)] = [c[(a, b, sign)], c[(b, a, -sign)]]

ans = 1 
used_cnt = double_zero_cnt 
for key in d.keys():
    if d[key] == []: 
        continue
    x, y = d[key]
    used_cnt += (x + y)
    mul = (pow(2, x, mod) + pow(2, y, mod) - 1) % mod 
    ans *= mul
    ans %= mod

ans *= pow(2, n - (used_cnt), mod) 
ans += double_zero_cnt 
ans -= 1 
ans %= mod
print(ans)