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



Sample input:
3
1 2
-1 1
2 -1


Sample output:
5

Submission status: Accepted

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
import sys
gcd = math.gcd
co = collections.Counter()
n = ni()
mod=10**9+7
for i in range(n):
    a,b = ma()
    d = gcd(a,b)
    if d!=0:
        a//=d
        b//=d
    elif abs(a)>0 or abs(b)>0:
        l = max(abs(a), abs(b))
        a//=l
        b//=l
    co[(a,b)]+=1
d = collections.defaultdict(lambda:False)
ans=1
for t,cnt in co.items():
    a,b = t
    if t==(0,0) or d[t]:
        continue
    d[(a,b)]=True;d[(-a,-b)]=True;d[(b,-a)]=True;d[(-b,a)]=True
    l1 = co[(a,b)]+co[(-a,-b)]
    l2 = co[(b,-a)]+co[(-b,a)]
    ans=ans*(pow(2,l1,mod)+pow(2,l2,mod) -1)%mod
    ans%=mod
print((ans-1 +co[(0,0)])%mod)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
z = 0
d = defaultdict(int)
for _ in range(n):
    a,b = inpl()
    if a == 0 and b == 0:
        z += 1
        continue
    if a == 0: b = 1
    if b == 0: a = 1
    if b < 0: a,b = -a,-b
    g = math.gcd(a,b)
    a //= g; b //= g
    d[(a,b)] += 1
res = 1
seen = set()
for k in list(d):
    a,b = k
    if k in seen: continue
    if a < 0: x,y = b,-a
    else: x,y = -b,a
    if a == 0: x,y = 1,0
    if b == 0: x,y = 0,1
    res *= pow(2,d[(a,b)],mod) + pow(2,d[(x,y)],mod) -1
    res %= mod
    # print(res)
    seen.add(k); seen.add((x,y))
res += z - 1
print(res%mod)

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import gcd

mod = 10 ** 9 + 7

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

d = defaultdict(int)
zero = 0
for a, b in ab:
    # zero　別処理
    if a == b == 0:
        zero += 1
        continue
    if a == 0:
        d[(0, 1)] += 1
        continue
    if b == 0:
        d[(1, 0)] += 1
        continue

    # a[i]が絶対に非負になるように
    if a < 0:
        a *= -1
        b *= -1
    g = gcd(a, b)
    d[(a // g, b // g)] += 1

all = n - zero
ans = 1
for k1, v in d.items():
    k2 = (k1[1], -k1[0])
    if k2 in d.keys():
        all -= v
        all -= d[k2]
        ans = ans * (pow(2, v, mod) + pow(2, d[k2], mod) - 1) % mod
ans = ans * pow(2, all, mod) % mod
print((ans - 1 + zero) % mod)

Submission status: Accepted

Solution code:
mod = 1000000007

N = int(input())

pos = []
neg = []
zer = []
inf = []
sol = 0

###除外すべき組み合わせの個数を数える
NG = []

for i in range(N):
    a,b = map(int,input().split())
    if a*b > 0:
        pos.append((abs(a),abs(b)))
    elif a*b < 0:
        neg.append((-abs(a),abs(b)))
    elif a == 0 and b != 0:
        inf.append((0,b))
    elif a != 0 and b == 0:
        zer.append((a,0))
    else:
        sol += 1

NG.append((len(inf),len(zer)))
free = len(pos)+len(neg)


# 以下しばらくneg,posに含まれるものに関する議論

pos.sort(key=lambda x:(x[1]/x[0]))
neg.sort(key=lambda x:(x[1]/x[0]))

x = 0
y = 0


while (x < len(pos) and y < len(neg)):
    if pos[x][0]*neg[y][0]+pos[x][1]*neg[y][1] == 0:
        s = 1
        t = 1
        while x+1 < len(pos) and pos[x+1][0]*neg[y][0]+pos[x+1][1]*neg[y][1] == 0:
            x += 1
            s += 1
        while y+1 < len(neg) and pos[x][0]*neg[y+1][0]+pos[x][1]*neg[y+1][1] == 0:
            y += 1
            t += 1
        NG.append((s,t))
        x += 1
        y += 1
        free -= (s+t)
        continue


    if x >= len(pos)-1 and y >= len(neg)-1:
        break

    elif y+1 < len(neg) and pos[x][0]*neg[y][0]+pos[x][1]*neg[y][1] > 0:
        y += 1
    elif x+1 < len(pos) and pos[x][0]*neg[y][0]+pos[x][1]*neg[y][1] < 0:
        x += 1
    elif x >= len(pos)-1:
        y += 1
    elif y >= len(neg)-1:
        x += 1

ans = pow(2,free,mod)

for s,t in NG:
    # s個のうち1個以上入れる or t個のうち1個以上入れる or どちらも1つも入れない
    ans *= pow(2,s,mod)-1 +pow(2,t,mod)-1 +1
    ans %= mod

#print('free',free)
#print('NG',NG)

# 1つも選ばないものの除外、solは単体で選べばOK
print((ans+sol-1)%mod)

Submission status: Accepted

Solution code:
mod = 1000000007

N = int(input())

pos = []
neg = []
zer = []
inf = []
sol = 0

###除外すべき組み合わせの個数を数える
NG = []

for i in range(N):
    a,b = map(int,input().split())
    if a*b > 0:
        pos.append((abs(a),abs(b)))
    elif a*b < 0:
        neg.append((-abs(a),abs(b)))
    elif a == 0 and b != 0:
        inf.append((0,1))
    elif a != 0 and b == 0:
        zer.append((1,0))
    else:
        sol += 1

NG.append((len(inf),len(zer)))
free = len(pos)+len(neg)


# 以下しばらくneg,posに含まれるものに関する議論

pos.sort(key=lambda x:(x[1]/x[0]))
neg.sort(key=lambda x:(x[1]/x[0]))

x = 0
y = 0


while (x < len(pos) and y < len(neg)):
    if pos[x][0]*neg[y][0]+pos[x][1]*neg[y][1] == 0:
        s = 1
        t = 1
        while x+1 < len(pos) and pos[x+1][0]*neg[y][0]+pos[x+1][1]*neg[y][1] == 0:
            x += 1
            s += 1
        while y+1 < len(neg) and pos[x][0]*neg[y+1][0]+pos[x][1]*neg[y+1][1] == 0:
            y += 1
            t += 1
        NG.append((s,t))
        x += 1
        y += 1
        free -= (s+t)
        continue


    if x >= len(pos)-1 and y >= len(neg)-1:
        break

    elif y+1 < len(neg) and pos[x][0]*neg[y][0]+pos[x][1]*neg[y][1] > 0:
        y += 1
    elif x+1 < len(pos) and pos[x][0]*neg[y][0]+pos[x][1]*neg[y][1] < 0:
        x += 1
    elif x >= len(pos)-1:
        y += 1
    elif y >= len(neg)-1:
        x += 1

ans = pow(2,free,mod)

for s,t in NG:
    # s個のうち1個以上入れる or t個のうち1個以上入れる or どちらも1つも入れない
    ans *= pow(2,s,mod)-1 +pow(2,t,mod)-1 +1
    ans %= mod

#print('free',free)
#print('NG',NG)

# 1つも選ばないものの除外、solは単体で選べばOK
print((ans+sol-1)%mod)

Submission status: Accepted

Solution code:
import sys
from math import gcd
from collections import defaultdict


def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())


N = I()
mod = 10**9+7
plus = defaultdict(int)
minus = defaultdict(int)
flag = defaultdict(int)
A_zero,B_zero = 0,0
zero_zero = 0
for i in range(N):
    a,b = MI()
    if a == b == 0:
        zero_zero += 1
    elif a == 0:
        A_zero += 1
    elif b == 0:
        B_zero += 1
    else:
        if a < 0:
            a,b = -a,-b
        a,b = a//gcd(a,b),b//gcd(a,b)
        if b > 0:
            plus[(a,b)] += 1
        else:
            minus[(a,b)] += 1
            flag[(a,b)] = 0

ans = 1

for a,b in plus.keys():
    ans *= pow(2,plus[(a,b)],mod)+pow(2,minus[(b,-a)],mod)-1
    ans %= mod
    flag[(b,-a)] = 1

for key in minus.keys():
    if flag[key] == 0:
        ans *= pow(2,minus[key],mod)
        ans %= mod

ans *= pow(2,A_zero,mod)+pow(2,B_zero,mod)-1
ans %= mod

ans += zero_zero-1
ans %= mod

print(ans)

Submission status: Accepted

Solution code:
import sys
from math import gcd
from collections import defaultdict

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N = I()
mod = 10**9+7
plus = defaultdict(int)
minus = defaultdict(int)
flag = defaultdict(int)
A_zero,B_zero = 0,0
zero_zero = 0
for i in range(N):
    a,b = MI()
    if a == b == 0:
        zero_zero += 1
    elif a == 0:
        A_zero += 1
    elif b == 0:
        B_zero += 1
    else:
        if a < 0:
            a,b = -a,-b
        a,b = a//gcd(a,b),b//gcd(a,b)
        if b > 0:
            plus[(a,b)] += 1
        else:
            minus[(a,b)] += 1
            flag[(a,b)] = 1

ans = 1

for a,b in plus.keys():
    ans *= pow(2,plus[(a,b)],mod)+pow(2,minus[(b,-a)],mod)-1
    ans %= mod
    flag[(b,-a)] = 0

for key in minus.keys():
    if flag[key] == 1:
        ans *= pow(2,minus[key],mod)
        ans %= mod

ans *= pow(2,A_zero,mod)+pow(2,B_zero,mod)-1
ans %= mod

ans += zero_zero-1
ans %= mod

print(ans)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
from math import gcd
from collections import defaultdict

n = int(input())
plus = defaultdict(int)
minus = defaultdict(int)
zero = [0, 0, 0]
for _ in range(n):
    a, b = map(int, input().split())
    if a * b == 0:
        if a == b == 0:
            zero[0] += 1
        elif a == 0:
            zero[1] += 1
        else:
            zero[2] += 1
        continue
    g = gcd(a, b)
    a //= g
    b //= g
    if a < 0:
        a *= -1
        b *= -1
    if b >= 0:
        s = str(a) + "-" + str(b)
        plus[s] += 1
    else:
        s = str(-b) + "-" + str(a)
        minus[s] += 1

pm = [[zero[1], zero[2]]]
cnt = sum(zero)
for key, value in plus.items():
    if minus[key] != 0:
        pm.append([value, minus[key]])
        cnt += value + minus[key]
        
ans = pow(2, n - cnt, MOD)

for p, m in pm:
    ans *= (pow(2, p, MOD) + pow(2, m, MOD) - 1)
    ans %= MOD
ans += zero[0] - 1
ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import math

kMod = 1000000007

N = int(input())
key2count = defaultdict(lambda: [0, 0])

for _ in range(N):
    a, b = map(int, input().split())
    g = math.gcd(a, b)
    if a < 0 or a == 0 and b < 0:
        a, b = -a, -b
    if g > 0:
        a, b = a//g, b//g
    idx = 0
    if b <= 0:
        idx = 1
        a, b = -b, a
    key2count[(a, b)][idx] += 1

ans = 1
for key, val in key2count.items():
    if key == (0, 0):
        continue
    plus, minus = val
    ans *= (pow(2, plus, kMod) + pow(2, minus, kMod)-1)
    ans %= kMod

ans += sum(key2count[(0, 0)])

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

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import gcd as g
n = int(input())
mod = 10**9+7
sardines = set()
Ddic = defaultdict(int)
ans = 1
zcount = 0
for i in range(n):
  a,b = map(int,input().split())
  if a == 0 and b == 0:
    zcount += 1
    continue

  if a == 0:
    Ddic[(0,1)] += 1
    sardines.add((0,1))
    continue

  if b == 0:
    sardines.add((1,0))
    Ddic[(1,0)] += 1
    continue

  A,B = abs(a),abs(b)
  G = g(A,B)
  a //= G
  b //= G

  if b < 0:
    a = -a
    b = -b

  Ddic[(a,b)] += 1
  sardines.add((a,b))

for i in sardines:
  if i == (0,0) or (i[0] <= 0 and Ddic[(i[1],-i[0])] != 0):continue
  if i[0] <= 0:
    ans *= (2**Ddic[i] + 2**(Ddic[(i[1],-i[0])]))-1
  else:
    ans *= (2**Ddic[i] + 2**(Ddic[(-i[1],i[0])]))-1

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

Submission status: Accepted

Solution code:
import math
n = int(input())
from collections import defaultdict
d = defaultdict(list)
s = set()
zc = 0
for i in range(n):
    a,b = map(int, input().split())
    g = math.gcd(a,b)
    if g!=0:
        a //= g; b //= g
        if a<0:
            a,b = -a, -b
        elif a==0 and b<0:
            b *= -1
        d[a,b].append(i)
    if a==b==0:
        zc += 1
v = 1
s = set()
M = 10**9+7
for (a,b),val in d.items():
    aa,bb = b,-a
    if aa<0:
        aa,bb = -aa, -bb
    elif aa==0 and bb<0:
        bb *= -1
    if (a,b) in s or (aa,bb) in s:
        continue
    if (aa,bb) in d.keys():
        v *= (pow(2, len(val), M) + pow(2, len(d[aa,bb]), M) - 1)
    else:
        v *= pow(2, len(val), M)
    v %= M
    s.add((a,b))
    s.add((aa,bb))
v -= 1
v += zc
print(v%M)

Submission status: Accepted

Solution code:
from math import gcd
from bisect import bisect_left, bisect_right

MOD = 10 ** 9 + 7
pow2 = [1] * (2 * 10 ** 5 + 1)
for i in range(1, 2 * 10 ** 5 + 1):
	pow2[i] = (pow2[i - 1] * 2) % MOD

N = int(input())
zero = 0
iwasies = []
for i in range(N):
	A, B = map(int, input().split())
	if A == 0 and B == 0:
		zero += 1
	elif A == 0:
		iwasies.append((0, 1))
	elif B == 0:
		iwasies.append((1, 0))
	else:
		g = gcd(A, B)
		A //= g
		B //= g
		if A < 0:
			iwasies.append((-A, -B))
		else:
			iwasies.append((A, B))

numiwasi = len(iwasies)
iwasies.sort()
badpairs = set()
for i in range(numiwasi):
	A, B = iwasies[i]
	abcount = bisect_right(iwasies, (A, B)) - bisect_left(iwasies, (A, B))
	
	Ao, Bo = B, -A
	if Ao < 0:
		Ao, Bo = -Ao, -Bo
	
	aobocount = bisect_right(iwasies, (Ao, Bo)) - bisect_left(iwasies, (Ao, Bo))
	
	if aobocount > 0:
		if (A, B) < (Ao, Bo):
			badpairs.add(((A, B, abcount), (Ao, Bo, aobocount)))
		else:
			badpairs.add(((Ao, Bo, aobocount), (A, B, abcount)))

sociable = numiwasi
ans = 1

for bp in badpairs:
	count1 = bp[0][2]
	count2 = bp[1][2]
	sociable -= (count1 + count2)
	ans = (ans * (pow2[count1] + pow2[count2] - 1)) % MOD
ans = (ans * pow2[sociable]) % MOD

print((ans + zero - 1) % MOD)

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import gcd
N = int(input())
MOD = 10**9 + 7
L = defaultdict(lambda: [0,0])
zero = 0
for _ in range(N):
    A, B = map(int, input().split())
    if A==0: d=B
    elif B==0: d=A
    else: d=gcd(A,B)
    if d != 0:
        A, B = A//d, B//d
        if A<0: A, B = -A, -B
        if B>0: L[(A,B)][0] += 1
        else: L[(-B,A)][1] += 1
    else: zero += 1

res = 1

for x,y in L.values():
    res *= pow(2,x,MOD) + pow(2,y,MOD) -1
    res %= MOD
print((res+zero-1)%MOD)

Submission status: Accepted

Solution code:
import fractions
import sys
sys.setrecursionlimit(10**9)

MOD = 1000000007  # type: int
INF=10**20

class Counter:
    def __init__(self):
        self.dict = {}

    def add(self,x):
        if x in self.dict: self.dict[x] += 1
        else: self.dict[x] = 1

    def decrement(self,x):
        self.dict[x] -= 1
        if self.dict[x] <= 0:
            del self.dict[x]

    def get_dict(self):
        return self.dict



class Counting():
    def __init__(self,maxim,mod):
        maxim += 1

        self.mod = mod
        self.fact = [0]*maxim
        self.fact[0] = 1
        for i in range(1,maxim):
            self.fact[i] = self.fact[i-1] * i % mod

        self.invfact = [0]*maxim
        self.invfact[maxim-1] = pow(self.fact[maxim-1],mod-2,mod)
        for i in reversed(range(maxim-1)):
            self.invfact[i] = self.invfact[i+1] * (i+1) % mod


    def nCk(self,n,r):
        if n < 0 or n < r: return 0
        return self.fact[n] * self.invfact[r] * self.invfact[n-r] % self.mod

    def nPk(self,n,r):
        if n < 0 or n < r: return 0
        return self.fact[n] * self.invfact[n-r] % self.mod

        


def solve(N: int, A: "List[int]", B: "List[int]"):

    both_zero = 0
    a_zero = 0
    b_zero = 0
    
    counter = Counter()
    for i in range(N):
        a,b=A[i],B[i]   

        if a * b == 0:
            if a == 0 and b == 0:
                both_zero += 1
                continue
            elif a == 0:
                a,b = 0,1
            else:
                a,b = -1,0

        
        gcd = fractions.gcd(a,b)
        a //= gcd
        b //= gcd

        # (a,b): -はaの方に寄せる
        if a < 0 and b < 0:
            a *= -1
            b *= -1
        if a < 0 and b > 0:
            pass

        if a > 0 and b < 0:
            a *= -1
            b *= -1
        
        counter.add((a,b))

    
    seen = set()
    cannot = 0
    counter = counter.get_dict()

    ans = 1
    for e,count in counter.items():
        a,b = e
        if e in seen: continue
        # print(e)
    
        if a > 0:
            b *= -1
        else:
            a *= -1

        if (b,a) in seen: continue

        if (b,a) in counter:
            count2 = counter[(b,a)]

            ans *= pow(2,count,MOD) + pow(2,count2,MOD) - 1
        else:
            ans *= pow(2,count,MOD)
        
        ans %= MOD
        seen.add((b,a))
        # print(ans)
        
    # ans *= pow(2,a_zero,MOD) + pow(2,b_zero,MOD) - 1
    ans %= MOD

    ans += both_zero - 1
    ans %= MOD

    print(ans)


    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, A, B)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import gcd
N = int(input())
MOD = 10**9 + 7
L = defaultdict(lambda: [0,0])
zero = 0
for _ in range(N):
    A, B = map(int, input().split())
    if A==0: d=B
    elif B==0: d=A
    else: d=gcd(A,B)
    if d != 0:
        A, B = A//d, B//d
        if A<0: A, B = -A, -B
        if B>0: L[(A,B)][0] += 1
        else: L[(-B,A)][1] += 1
    else: zero += 1
res = 1
for x,y in L.values():
    res *= pow(2,x,MOD) + pow(2,y,MOD) -1
    res %= MOD
print((res+zero-1)%MOD)

Submission status: Accepted

Solution code:
def numba_compile(numba_config):
    import os, sys
    if sys.argv[-1] == "ONLINE_JUDGE":
        from numba import njit
        from numba.pycc import CC
        cc = CC("my_module")
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature)(func)
            cc.export(func.__name__, signature)(func)
        cc.compile()
        exit()
    elif os.name == "posix":
        exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
        for func, _ in numba_config:
            globals()[func.__name__] = vars()[func.__name__]
    else:
        from numba import njit
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature, cache=True)(func)
        print("compiled!", file=sys.stderr)

import sys
import numpy as np
from math import gcd
mod = 10**9+7
def pow_mod(base, exp):  # [pow_mod, "i8(i8,i8)"],
    # mod はグローバル変数を参照
    exp %= mod - 1
    res = 1
    while exp:
        if exp % 2:
            res = res * base % mod
        base = base * base % mod
        exp //= 2
    return res

def solve(N, AB):
    n = 0
    na = nb = nab = 0
    Cnt = {}
    for a, b in zip(AB[::2], AB[1::2]):
        if (a, b) == (0, 0):
            nab += 1
        elif a == 0:
            na += 1
        elif b == 0:
            nb += 1
        else:
            if b < 0:
                a, b = -a, -b
            g = gcd(a, b)
            a //= g
            b //= g
            ab_fr = (a, b)
            n += 1
            if ab_fr in Cnt:
                Cnt[ab_fr] += 1
            else:
                Cnt[ab_fr] = 1
            inv = (-b, a) if a > 0 else (b, -a)
            if inv not in Cnt:
                Cnt[inv] = 0
    ans = (pow_mod(2, na) + pow_mod(2, nb) - 1) % mod
    A = []
    for ab_fr, cnt in Cnt.items():
        a, b = ab_fr
        inv = (-b, a) if a > 0 else (b, -a)
        cnt2 = Cnt[inv]
        p = pow_mod(2, cnt)
        p2 = pow_mod(2, cnt2)
        A.append((p + p2 - 1) % mod)
    A.sort()
    for a in A[::2]:
        ans = ans * a % mod
    ans += nab - 1
    return ans % mod

numba_compile([
    [pow_mod, "i8(i8,i8)"],
    [solve, "i8(i8,i8[:])"],
])

def main():
    N = int(sys.stdin.buffer.readline())
    m = np.array(sys.stdin.buffer.read().split(), dtype=np.int64)
    ans = solve(N, m)
    print(ans)

main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


# 有理数クラス
class MyFraction:

    def __init__(self, a, b):
        assert b != 0, "分母が0です"

        if b < 0:
            a, b = -a, -b
        if a == 0:
            b = 1

        g = math.gcd(a, b)
        self.numerator = a // g
        self.denominator = b // g

    def __str__(self):
        return "{0}/{1}".format(self.numerator, self.denominator)


def main():
    N = NI()
    AB = defaultdict(int)
    Y, Z = 0, 0
    for i in range(N):
        a, b = NMI()
        if a == b == 0:
            Z += 1
            continue
        if b == 0:
            Y += 1
            continue

        if str(MyFraction(a, b)) in AB:
            AB[str(MyFraction(a, b))] += 1
        else:
            AB[str(MyFraction(a, b))] = 1

    C = defaultdict(lambda: ())
    ans = 1
    flag = True
    for ab, k in list(AB.items()):
        a, b = ab.split("/")
        a = int(a)
        b = int(b)
        if a > 0:
            if str(MyFraction(-b, a)) in AB:
                t = AB[str(MyFraction(-b, a))]
            else:
                t = 0
        elif a < 0:
            if str(MyFraction(b, -a)) in AB:
                continue
            t = 0
        else:
            flag = False
            t = Y
        C[(a, b)] = (k, t)

    for k, t in list(C.values()):
        tmp = pow(2, k, MOD) + pow(2, t, MOD) - 1
        ans = ans * tmp % MOD

    if flag:
        tmp = pow(2, Y, MOD)
        ans = ans * tmp % MOD
    print((ans + Z - 1)%MOD)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
from itertools import groupby, accumulate, product, permutations, combinations
from math import gcd

def reduction(x,y):
    g = gcd(x,y)
    return abs(x)//g,abs(y)//g

def solve():
    mod = 10**9+7
    dplus = defaultdict(lambda: 0)
    dminus = defaultdict(lambda: 0)
    N = int(input())
    x0,y0,xy0 = 0,0,0
    for i in range(N):
        x,y = map(int, input().split())
        if x==0 and y==0:
            xy0 += 1
        elif x==0 and y!=0:
            x0 += 1
        elif y==0:
            y0 += 1
        elif x*y>0:
            dplus[reduction(x,y)] += 1
        else:
            dminus[reduction(-y,x)] += 1
    ans = pow(2,x0,mod)+pow(2,y0,mod)-1
    other = N-x0-y0-xy0
    for k,v in dplus.items():
        ans *= pow(2,dminus[k],mod)+pow(2,v,mod)-1
        other -= dminus[k]+v
    ans *= pow(2,other,mod)
    ans += xy0-1
    ans %= mod
    return ans
print(solve())

Submission status: Accepted

Solution code:
from operator import itemgetter
from math import gcd
from collections import Counter

N = int(input())
mod = int(1e9+7)

zero = 0
P = {}
for i in range(N):
  a,b = list(map(int, input().split()))
  if a == 0 and b == 0:
    zero += 1
    continue
  g = gcd(a,b)
  a,b = a//g, b//g
  if b < 0:
    a,b = -a,-b
  if b == 0 and a < 0:
    a,b = -a,b  
  #rot90
  rot90 = a<=0
  if rot90:
    a,b = b,-a
  #add
  if not (abs(a),b) in P:
    P[(a,b)] = [0,0]
  if not rot90 :
    P[(a,b)][0] += 1
  else:
    P[(a,b)][1] += 1
    
ans = 1
for k,v in P.items():
  #print(k,v)
  s,t = v
  wk = 1 + pow(2,s,mod) - 1 + pow(2,t,mod) - 1
  ans *= wk
  ans %= mod
  
ans += zero

print((ans - 1) % mod)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

N = int(input())
zero = 0
d = defaultdict(lambda: [0, 0])
for i in range(N):
    x, y = map(int, input().split())
    if x == 0 and y == 0:
        zero += 1
        continue
    g = gcd(x, y)
    x //= g
    y //= g
    if y < 0:
        x = -x
        y = -y
    if y == 0 and x < 0:
        x = -x
    rotate = x <= 0
    if rotate:
        x, y = y, -x
        d[(x, y)][0] += 1
    else:
        d[(x, y)][1] += 1

ans = 1
for v in d.values():
    now = 1
    now += (2 ** v[0] - 1) % MOD
    now += (2 ** v[1] - 1) % MOD
    ans *= now
    ans %= MOD
print((ans - 1 + zero) % MOD)

Submission status: Accepted

Solution code:
from math import gcd

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

d = {}
for a, b in X:
    if a == b == 0:
        pass
    elif a == 0:
        b = 1
    elif b == 0:
        a = 1
    else:
        v = gcd(a, b)
        a //= v
        b //= v

    if a != 0 and a * b >= 0:
        pair = (abs(a), abs(b))
        if pair not in d:
            d[pair] = [0, 0]
        d[pair][0] += 1
    else:
        pair = (abs(b), abs(a))
        if pair not in d:
            d[pair] = [0, 0]
        d[pair][1] += 1

MOD = 10 ** 9 + 7
zero = 0
ans = 1
for p in d:
    if p == (0, 0):
        zero = d[p][1]
    else:
        a, b = d[p]
        ans *= pow(2, a, MOD) + pow(2, b, MOD) - 1
        ans %= MOD

print((ans + zero - 1) % MOD)

Submission status: Accepted

Solution code:

from math import gcd

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

d = {}
for a, b in X:
    if a == b == 0:
        pass
    elif a == 0:
        if b > 0:
            b = 1
        else:
            b = -1
    elif b == 0:
        if a > 0:
            a = 1
        else:
            a = -1
    else:
        v = gcd(a, b)
        a //= v
        b //= v

    if a != 0 and a * b >= 0:
        pair = (abs(a), abs(b))
        if pair not in d:
            d[pair] = [0, 0]
        d[pair][0] += 1
    else:
        pair = (abs(b), abs(a))
        if pair not in d:
            d[pair] = [0, 0]
        d[pair][1] += 1

MOD = 10 ** 9 + 7
zero = 0
ans = 1
for p in d:
    if p == (0, 0):
        zero = d[p][1]
    else:
        a, b = d[p]
        ans *= pow(2, a, MOD) + pow(2, b, MOD) - 1
        ans %= MOD

print((ans + zero - 1) % MOD)

Submission status: Accepted

Solution code:
from math import gcd
from collections import defaultdict as d
n=int(input())
m=10**9+7
s=d(lambda:[0,0])
p=-1
for i in range(n):
    a,b=map(int,input().split())
    if a==b==0:p+=1
    else:
        g=gcd(a,b)
        a=a//g
        b=b//g
        if a<0 or a==0<b:a,b=-a,-b
        if b<0:s[(-b,a)][1]+=1
        else:s[(a,b)][0]+=1
q=1
for i in s:
    k,l=s[i]
    q=q*(pow(2,k,m)+pow(2,l,m)-1)%m
print((q+p)%m)

Submission status: Accepted

Solution code:
from math import gcd
N=int(input())
a,b,g=0,0,0
X=[]
zeroandzero=0
D=dict()
for i in range(N):
  a,b=map(int,input().split())
  if a==b==0:
    zeroandzero+=1
    N-=1
    continue
  g=gcd(a,b)
  a//=g
  b//=g
  if b<0:
    a,b=-a,-b
  D[(a,b)]=D.get((a,b),0)+1
E=dict()
K=list(D.keys())
mod=10**9+7
inv2=(mod+1)//2
P=pow(2,N,mod)
for i in range(len(K)):
  if E.get(K[i],0):
    continue
  g=list(K[i])
  if g[0]<0:
    g=(K[i][1],-K[i][0])
  else:
    g=(-K[i][1],K[i][0])
  E[g]=1
  g=(D[K[i]],D.get(g,0))
  if g[1]:
    P=P*pow(inv2,g[0]+g[1],mod)*(pow(2,g[0],mod)+pow(2,g[1],mod)-1)%mod
print((P-1+zeroandzero)%mod)

Submission status: Accepted

Solution code:
N=int(input())
AB=[list(map(int,input().split())) for i in range(N)]
from collections import defaultdict
d=defaultdict(int)
d2=defaultdict(int)
a0=0
b0=0
c0=0
from math import gcd
for a,b in AB:
    if a==b==0:
        c0+=1
    elif a==0:
        a0+=1
    elif b==0:
        b0+=1
    else:
        if a<0:
            a,b=-a,-b
        g=gcd(a,b)
        a,b=a//g,b//g
        if b>0:
            d[(a,b)]+=1
        else:
            d2[(-b,a)]+=1
m=10**9+7
a=1
for k,v in d.items():
    if k in d2:
        a=a*(pow(2,v,m)+pow(2,d2[k],m)-1)%m
    else:
        a=a*pow(2,v,m)%m
for k,v in d2.items():
    if k not in d:
        a=a*pow(2,v,m)%m
print((a*(pow(2,a0,m)+pow(2,b0,m)-1)-1+c0)%m)

Submission status: Accepted

Solution code:

from math import gcd
from collections import defaultdict

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

d = {}
for a, b in X:
    if a == b == 0:
        pass
    elif a == 0:
        if b > 0:
            b = 1
        else:
            b = -1
    elif b == 0:
        if a > 0:
            a = 1
        else:
            a = -1
    else:
        v = gcd(a, b)
        a //= v
        b //= v

    if a != 0 and a * b >= 0:
        pair = (abs(a), abs(b))
        if pair not in d:
            d[pair] = [0, 0]
        d[pair][0] += 1
    else:
        pair = (abs(b), abs(a))
        if pair not in d:
            d[pair] = [0, 0]
        d[pair][1] += 1

MOD = 10 ** 9 + 7
zero = 0
ans = 1
for p in d:
    if p == (0, 0):
        zero = d[p][1]
    else:
        a, b = d[p]
        ans *= pow(2, a, MOD) + pow(2, b, MOD) - 1
        ans %= MOD

print((ans + zero - 1) % MOD)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
from fractions import Fraction
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


MOD = 1000000007


def func(v1, v2):
    ans = 1
    ans += pow(2, v1, MOD) - 1
    ans += pow(2, v2, MOD) - 1

    return ans % MOD


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

    num_zero = 0

    d = defaultdict(lambda: [0, 0])
    for _ in range(N):
        A, B = map(int, input().split())
        if A == 0 and B == 0:
            num_zero += 1
            continue
        elif A == 0:
            d[(0, 0)][0] += 1
        elif B == 0:
            d[(0, 0)][1] += 1
        else:
            g = gcd(abs(A), abs(B))
            A //= g
            B //= g
            if B > 0:
                if A > 0:
                    d[(A, B)][1] += 1
                else:
                    d[(B, -A)][0] += 1
            else:
                if A < 0:
                    d[(-A, -B)][1] += 1
                else:
                    d[(-B, A)][0] += 1

    ans = 1
    for k, v in d.items():
        ans *= func(v[0], v[1])
        ans %= MOD

    print((ans + num_zero + MOD - 1) % MOD)


def main():
    solve()


if __name__ == '__main__':
    main()