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: Runtime Error

Solution code:
N = int(input())
P = [[int(i) for i in input().split()] for  _ in range(N)]

import numpy as np
from collections import deque

Pmat = np.matrix(P)


badmat = Pmat@Pmat.T
badmat = np.where(badmat ==0)


graph = [deque([]) for _ in range(N + 1)]
for i in range(N):
    graph[i].extend(badmat[1][badmat[0]==i])


arrive_time = [0] * N # 到着時刻
visit = [-1]*(N)

# 深さ優先探索
def dfs(v):
    s = 1
    stack = [v]
    arrive_time[v] = 1
    while stack:
        v = stack[-1]
        if graph[v]:
            w = graph[v].popleft()
            if visit[w] == -1:
                visit[w] = 1
                stack.append(w)
        else:
            stack.pop()
    return s

ans=[]
# 孤立している頂点対策
for i in range(N):
    if visit[i] == -1:
        ans.append(dfs(i))

def power_func(a,n,p):
    bi = str(format(n,"b"))#2進表現に
    res = 1
    for i in range(len(bi)):
        res = (res*res) %p
        if bi[i] == "1":
            res = (res*a) %p
    return res


kotae = 0
for a in ans:
    kotae+=power_func(2,a,1000000007)
    kotae = kotae % 1000000007
print(kotae-1)

Submission status: Runtime Error

Solution code:
N = int(input())
A = []
B = []
C = {}
m = 1000000007

def mypow(dummy, num):

    ans = 1
    for i in range(num):
        ans = (ans*2)%1000000007
    return ans

for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
    if a/b in C.keys():
        C[a/b] +=1
    else: C[a/b] = 1
    
used = set()
ans = 1
for c in C.keys():
    if c in used: continue
    if -1/c in C.keys():
        used.add(-1/c)
        
        score = mypow(2, C[c]) + mypow(2, C[-1/c])- 1
        
        ans = (ans * score)%m

    else:
        
        ans = (ans*mypow(2, C[c]))%m

print(ans-1)

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
pr=10**9+7
a0=b0=c0=0
l=[]
for i in range(N):
    A,B=map(int,input().split())
    if A==0:
        if B:
            a0+=1
        else:
            c0+=1
    if B==0:
        b0+=1
    if A*B>0:
        l.append((A/B,A,B,0))
    else:
        l.append((-B/A,A,B,1))
l.sort()
N-=c0+a0+b0
a=pow(2,a0,pr)+pow(2,b0,pr)-1
s=t=0
if l[0][-1]:
    t+=1
else:
    s+=1
for i in range(N-1):
    if l[i+1][0]!=l[i][0]:
        a=a*(pow(2,s,pr)+pow(2,t,pr)-1)%pr
        N-=s+t
        s=t=0
    if l[i+1][-1]:
        t+=1
    else:
        s+=1
a=a*(pow(2,s,pr)+pow(2,t,pr)-1)%pr
N-=s+t
a=(a*(pow(2,N,pr))-1)%pr
print(a)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
MOD = 10**9+7
N = int(input())
bul = defaultdict(int)
bul2 = defaultdict(int)
for _ in range(N):
    a,b = map(int,input().split())
    bul[a/b] += 1
    bul2[-(b/a)] += 1

ALL = pow(2,N,MOD)-1

for k in bul.keys():
    if k <= 0:
        ALL -= bul2[k] * bul[k] * (N-1)
        ALL %= MOD

print(ALL)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
N = int(input())

A = []
B = []

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

cnt = 0
for i in range(N):
  for j in range(N):
    if A[i]*A[j] + B[i]*B[j] = 0:
      pass
    else:
      cnt += 1
print(cnt)

Submission status: Runtime Error

Solution code:
MAX_A = 10**18+1
DELIM = 1000000007

def kaijo(N):
    if(N==0):
        return 1
    if(N%2==1):
        return (2 * ((kaijo(2, N//2) ** 2)%DELIM)) % DELIM
    else:
        return ((kaijo(2, N//2) % DELIM) ** 2) % DELIM

def main():
    N = int(input())
    AB = [[0] * 2 for i in range(N)]
    X = [0] * N
    no_count_comb = 0
    A_Zeros = []
    B_Zeros = []
    X_Dict = dict()
    for i in range(N):
        A, B = map(int, input().split())
        if(A == 0):
            A_Zeros.append(i)
            no_count += len(B_Zeros)
        if(B == 0):
            B_Zeros.append(i)
            no_count += len(A_Zeros)
            X[i] = MAX_A

        else:
            X = A / B
            if(X in X_Dict):
                X_Dict[X] += 1
            else:
                X_Dict[X] = 1

            if(1 / X in X_Dict):
                no_count_comb += X_Dict[1/X]
        

    total = kaijo(N) - 1
    no_count = kaijo(N-2)
    print((total - no_count_comb * no_count_comb)%DELIM)

main()

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from decimal import Decimal
N = int(input())

MOD = 10 ** 9 + 7

T = defaultdict(int)

ans = 0
for i in range(N):
    a, b = list(map(int, input().split()))
    ans += 2 ** (i - T[Decimal(b)/Decimal(a)])
    ans %= MOD
    T[- Decimal(a)/Decimal(b)] += 1

print(ans)

Submission status: Runtime Error

Solution code:
MOD = 10**9 + 7


def main():
    N = int(input())
    AB = {tuple(sorted(int(i) for i in input().split())) for j in range(N)}
    cnt = 0
    ban = set()
    bannum = 0
    for a, b in AB:
        if (a, b) in ban:
            continue
        ban1 = (-a, b)
        ban2 = (a, -b)
        if ban1 in AB and ban2 in AB:
            ban.add(ban1)
            ban.add(ban2)
            cnt += 1
            bannum += 3*pow(2, N-3, MOD)
            bannum %= MOD
        elif ban1 in AB:
            ban.add(ban1)
            cnt += 1
            bannum += pow(2, N-2, MOD)
            bannum %= MOD
        elif ban2 in AB:
            ban.add(ban2)
            bannum += pow(2, N-2, MOD)
            bannum %= MOD
            cnt += 1
    if (-1, 1) in AB:
        add = 1
        for a, b in AB:
            if a == b:
                bannum += pow(2, N-2, MOD)
                bannum %= MOD
                add += 1
        cnt += add
    # cntは選んではいけないペアの数
    v = cnt
    ans = pow(2, N, MOD) - 1
    ans -= bannum
    ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(n,base=2):
    rt=[]
    for tb in range(base**n):s=[tb//(base**bt)%base for bt in range(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
 
YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
class Tree:
    def __init__(self,inp_size=None,ls=None,init=True,index=0):
        self.LCA_init_stat=False
        self.ETtable=[]
        if init:
            if ls==None:
                self.stdin(inp_size,index=index)
            else:
                self.size=len(ls)+1
                self.edges,_=GI(self.size,self.size-1,ls,index=index)
        return
 
    def stdin(self,inp_size=None,index=1):
        if inp_size==None:
            self.size=int(input())
        else:
            self.size=inp_size
        self.edges,_=GI(self.size,self.size-1,index=index)
        return
    
    def listin(self,ls,index=0):
        self.size=len(ls)+1
        self.edges,_=GI(self.size,self.size-1,ls,index=index)
        return
 
    def __str__(self):
        return  str(self.edges)
 
    def dfs(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0):
        q=deque()
        q.append(x)
        v=[-1]*self.size
        v[x]=root_v
        while q:
            c=q.pop()
            for nb,d in self.edges[c]:
                if v[nb]==-1:
                    q.append(nb)
                    v[nb]=func(v[c],nb,d)
        return v
 
    def EulerTour(self,x):
        q=deque()
        q.append(x)
        self.depth=[None]*self.size
        self.depth[x]=0
        self.ETtable=[]
        self.ETdepth=[]
        self.ETin=[-1]*self.size
        self.ETout=[-1]*self.size
        cnt=0
        while q:
            c=q.pop()
            if c<0:
                ce=~c
            else:
                ce=c
                for nb,d in self.edges[ce]:
                    if self.depth[nb]==None:
                        q.append(~ce)
                        q.append(nb)
                        self.depth[nb]=self.depth[ce]+1
            self.ETtable.append(ce)
            self.ETdepth.append(self.depth[ce])
            if self.ETin[ce]==-1:
                self.ETin[ce]=cnt
            else:
                self.ETout[ce]=cnt
            cnt+=1
        return
    
    def LCA_init(self,root):
        self.EulerTour(root)
        self.st=SparseTable(self.ETdepth,init_func=min,init_idl=inf)
        self.LCA_init_stat=True
        return
    
    def LCA(self,root,x,y):
        if self.LCA_init_stat==False:
            self.LCA_init(root)
        xin,xout=self.ETin[x],self.ETout[x]
        yin,yout=self.ETin[y],self.ETout[y]
        a=min(xin,yin)
        b=max(xout,yout,xin,yin)
        id_of_min_dep_in_et=self.st.query_id(a,b+1)
        return self.ETtable[id_of_min_dep_in_et]
 
class SparseTable: # O(N log N) for init, O(1) for query(l,r)
    def __init__(self,ls,init_func=min,init_idl=float('inf')):
        self.func=init_func
        self.idl=init_idl
        self.size=len(ls)
        self.N0=self.size.bit_length()
        self.table=[ls[:]]
        self.index=[list(range(self.size))]
        self.lg=[0]*(self.size+1)
        
        for i in range(2,self.size+1):
            self.lg[i]=self.lg[i>>1]+1
 
        for i in range(self.N0):
            tmp=[self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) for j in range(self.size)]
            tmp_id=[self.index[i][j] if self.table[i][j]==self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) else self.index[i][min(j+(1<<i),self.size-1)] for j in range(self.size)]
            self.table+=[tmp]
            self.index+=[tmp_id]
    
    # return func of [l,r)
    def query(self,l,r):
        if r>self.size:r=self.size
        #N=(r-l).bit_length()-1
        N=self.lg[r-l]
        return self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))])
    
    # return index of which val[i] = func of v among [l,r)
    def query_id(self,l,r):
        if r>self.size:r=self.size
        #N=(r-l).bit_length()-1
        N=self.lg[r-l]
        a,b=self.index[N][l],self.index[N][max(0,r-(1<<N))]
        if self.table[0][a]==self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))]):
            b=a
        return b
    
    def __str__(self):
        return str(self.table[0])
 
    def print(self):
        for i in self.table:
            print(*i)
 
class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]
 
    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo

def dijkstra(edge,st):
    # edge=[[(v_to,dist_to_v),...],[],...]
    # initialize: def: d=dist(st,i), prev=[previous vertex in minimum path], q[]
    n=len(edge)
    d=[(0 if st==i else inf) for i in range(n)]
    prev=[0]*n
    q=[(j,i) for i,j in enumerate(d)]
    heapify(q)
    v=[False]*n
    # calc
    while q:
        dist,cur=heappop(q)
        if v[cur]:
            continue
        v[cur]=True
        for dst,dist in edge[cur]:
            alt=d[cur]+dist
            if alt<d[dst]:
                d[dst]=alt
                prev[dst]=cur
                heappush(q,(alt,dst))
    return d,prev


show_flg=False
show_flg=True
ans=0

n=I()
ans=pow(2,n,mo)-1

dp=[[0]*2 for i in range(n+1)]
dp[0][1]=1
m=defaultdict(int)
L=10**19
for i in range(n):
    a,b=LI()

    g=gcd(a,b)
    x,y=a//g,b//g
    if y<0:
        x*=-1
        y*=-1

    m[x*L+y]+=1
    u,v=-y,x
    if v<0:
        u*=-1
        v*=-1
    kp=x*L+y
    kq=u*L+v
    p=m[x*L+y]
    q=0
    if kq in m:
        q=m[kq]
    dp[i+1][0]=sum(dp[i])
    ng=1
    #ng*=pow(2,p-1,mo)
    ng*=(pow(2,q,mo)-1)
    ng*=pow(2,i-(p-1)-q,mo)
    dp[i+1][1]=sum(dp[i])-ng%mo
    
    #show(a,b,g,(x,y),x*L+y,ng)

#print('DP',dp)
ans=sum(dp[n])-1

print(ans)


inv=pow(2,mo-2,mo)
z=[]
nz=0
for i,j in m.items():
    x,y=i//L,i%L
    u,v=-y,x
    if v<0:
        u*=-1
        v*=-1
    kp=x*L+y
    kq=u*L+v
    p=m[x*L+y]
    if u*L+v in m:
        q=m[u*L+v]
    else:
        q=0
    les=(pow(2,p,mo)-1)*(pow(2,q,mo)-1)
    zz=(pow(2,p,mo)-1)*(pow(2,q,mo)-1)
    les%=mo

    
    les*=pow(2,n-p-q,mo)
    #show((i,j),(p,q),(kp,kq),ans)
    
    ans-=les//2
    ans%=mo
    
print(ans%mo)

Submission status: Runtime Error

Solution code:
import math

# Combination
MOD = 10**9+7
MAX = 2*10**5
fac = [1,1] + [0]*MAX
finv = [1,1] + [0]*MAX
inv = [0,1] + [0]*MAX
for i in range(2,MAX+2):
    fac[i] = fac[i-1] * i % MOD
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i-1] * inv[i] % MOD

def comb(n,r):
    if n < r: return 0
    if n < 0 or r < 0: return 0
    return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD

from collections import defaultdict

N = int(input())
dic = defaultdict(int)
gp = []
for i in range(N):
    a,b = map(int, input().split())
    g = math.gcd(a, b)
    a //= g
    b //= g
    dic[str(a)+":"+str(b)] += 1
    gp.append((a, b))
cnt = 0
print(dic)
for a,b in gp:
    if (a >= 0 and b >= 0) or (a <= 0 and b <= 0):
        cnt += dic[str(b)+":"+str(-a)]
        cnt += dic[str(-b)+":"+str(a)]
    else:
        cnt += dic[str(b)+":"+str(a)]
        cnt += dic[str(-b)+":"+str(-a)]
#print(cnt)
cnt //= 2
ans = N
for i in range(2,N+1):
    c = comb(N, i) - cnt*comb(N-2, i-2)
    c = max(0, c)
    #print(i, comb(N, i), cnt*comb(N-2, i-2))
    if c < 0:
        c += MOD
    ans += c
    ans %= MOD
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    from collections import Counter
    from math import gcd
    n, *ab = map(int, open(0).read().split())
    mod = 10 ** 9 + 7
    ans = pow(2, n, mod) - 1
    d = []

    for i, j in zip(*[iter(ab)] * 2):
        g = gcd(i, j)
        x = [i // g, j // g]
        d.append(x)

    vis = []

    tmp = 0
    for (i, j) in d:
        if [i, j] in vis:
            continue
        if [-j, i] in vis:
            continue
        if [j, -i] in vis:
            continue
        x = d.count([i, j])
        y = d.count([-j, i]) + d.count([j, -i])
        z = n - (x + y)
        if y == 0:
            continue
        i = pow(2, x, mod) - 1
        j = pow(2, y, mod) - 1
        k = pow(2, z, mod)
        tmp += i * j * k
        vis.append([i, j])
        vis.append([-j, i])
        vis.append([j, -i])

    ans -= (tmp // 2)
    tmp %= mod

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from fractions import Fraction


N = int(input())

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

MOD = 10**9 + 7

from collections import defaultdict

d = defaultdict(int)
ans = 1
cnt_00 = 0
for a,b in AB:
    if a == 0 and b == 0:
        cnt_00 += 1 
    elif a == b:
        d[1] += 1
    else:
        d[Fraction(a,b)] += 1




ans = 1
ks = list(d.keys())
for k in ks:
    if d[k] == 0:
        continue
    
    elif k == 1 or k == -1:

        num = (((pow(2,d[1], MOD) + pow(2, d[-1], MOD)) % MOD) -1) % MOD
        ans *= num
        ans %= MOD
        d[1] = 0
        d[-1] = 0

    else:

        num = (((pow(2, d[k], MOD) + pow(2,d[Fraction(-1, k)], MOD)) % MOD)-1) % MOD
        ans *= num
        ans %= MOD
        d[k] = 0
        d[Fraction(-1, k)] = 0
        



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

Submission status: Runtime Error

Solution code:
IN = iter(Input.split('\n')).__next__
def input():
    return IN()

# 入力
def cmb(n, r, mod):
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod
 
mod = 10**9+7  
nums = 2*(10**5) # 制約に合わせよう
g1, g2, inverse = [1, 1] , [1, 1], [0, 1]
 
for num in range(2, nums + 1):
    g1.append((g1[-1] * num) % mod)
    inverse.append((-inverse[mod % num] * (mod//num)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)

import bisect
n = int(input())
ab00 = []
ab01 = []
ab1 = []
ab2 = []
for _ in range(n):
    a, b = map(int, input().split())
    if (a<0 and b<0) or (a>0 and b>0):
        ab1.append([a, b])
    elif a == 0 and b == 0:
        ab00.append([a, b])
    elif a == 0 or b == 0:
        ab01.append([a, b])
    else:
        ab2.append([a,b])
number = 10**9
d1 = []
d2 = []
for i, j in ab1:
    if i < j: i, j = j, i
    d1.append(i/j)
d1.sort()
for i, j in ab2:
    if i < j: i, j = j, i
    d2.append(-1*i/j)
res = []
for i in range(len(ab2)):
    x = d2[i]
    index_left = bisect.bisect_left(d1, x)
    index_right = bisect.bisect_right(d1, x)
    if index_right-index_left:
        res.append(index_right-index_left)

mod = 10**9+7
ans = n
if n >= 2:
    ans += n*(n-1)//2 - sum(res)
if n >= 3:
    for i in range(3, n):
        if n-i >= max(res):
            ans += cmb(n, i, mod)
            for j in res:
                ans -= cmb(n-i-j+2, i-2, mod)
print(ans)

Submission status: Runtime Error

Solution code:
from decimal import Decimal, getcontext

getcontext().prec(100)
n = int(input())
nlist = {}
for i in range(n):
    a,b=map(int,input().split())
    div= Decimal(a)/Decimal(b) if b!=0 else "v"
    try:
        nlist[div]=nlist[div] +1
    except KeyError:
        nlist[div]=1
    
for i in nlist.keys():
    div=i
    if div=='v':
        to=Decimal(1)/div if div!=Decimal(0) else "v"
    else:
        to=Decimal(0)
    try:
        nlist[to]=nlist[to]*nlist[div]
    except KeyError:
        pass

ans=0
for i in nlist.values():
    ans+=i
print((2**n-ans)%1000000007)

Submission status: Runtime Error

Solution code:
def gcd(a,b):
    if b>a:
        a,b = b,a
    
    if b==0:
        return a
    else:
        return gcd(b,a%b)



N = int(input())

dic = {}

cnt = 0

for i in range(N):
    A,B = map(int,input().split())

    mm = gcd(abs(A),abs(B))
    A = A//mm
    B = B//mm

    A1 = min(abs(A),abs(B))
    B1 = max(abs(A),abs(B))

    mm2 = abs(A1)*10**19+abs(B1)
    mm2 *= A*B//abs(A1*B1)
    if mm2 in dic:
        dic[mm2] += 1
    else:
        dic[mm2] = 1

    if mm2*(-1) in dic:
        cnt += 1
    A,B = B,A
    mm2 = abs(A)*10**19+abs(B)
    mm2 *= A*B//abs(A*B)
    if mm2*(-1) in dic:
        cnt += 1
# print(dic)
mm = N
mod = 1000000007
ans = 1
for i in (dic):
    if (-1)*i in dic:
        mm -= dic[i]
        mm -= dic[(-1)*i]

        mm2 = pow(2,dic[i])%mod + pow(2,dic[(-1)*i])%mod
        ans *= mm2
        ans %mod

ans *= pow(2,mm)%mod
ans -= 1


print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from itertools import combinations

N = int(input())
D = defaultdict(float)
for _ in range(N):
    a, b = map(int, input().split())
    D[a / b] += 1
Keys = D.keys()

MOD = 1000000007
M = [0] * (N + 1) # X[i] 2のi乗 % MOD
M[0] = 1
for i in range(1, N + 1):
    M[i] = M[i - 1] * 2 % MOD

tmp = 0
for x, y in combinations(Keys, 2):
    if x + 1 / y == 0:
        s = int(D[x])
        t = int(D[y])
        tmp = (tmp + (M[s] - 1) * (M[t] - 1) * M[N - s - t]) % MOD
ans = (M[N] - 1 - tmp) % MOD
print(ans)

Submission status: Runtime Error

Solution code:
MOD=10**9+7
n=int(input())
p=[]
m=[]
for i in range(n):
    a,b=map(int,input().split())
    if a*b<0:
        m.append(a*b)
    else:
        p.append(a*b)
m.sort()
p=sorted(p,reverse=True)
r=0
l=0
cnt=0
while l<len(m) and r<len(p):
    if p[r]+m[l]>0:
        l+=1
    elif p[r]+m[l]<0:
        r+=1
    else:
        print(0)
        cnt+=1
        if len(m)-l<len(p)-r:
            r+=1
        else:
            l+=1
if l==len(m):
    cnt+=p.count(m[-1])
else:
    cnt+=m.count(p[-1])
print(cnt)
ans=(pow(2,n,MOD)-1-pow(2,n-2,MOD)*cnt)%MOD

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())

a_list=[]
b_list=[]
for i in range(N) :
  a,b=map(int,input().split())

  a_list.append(-a/b)
  b_list.append(b/a)

import collections
c=collections.Counter(a_list)
d=collections.Counter(b_list)
e=list(c.keys())
f=list(c.values())
g=list(d.keys())
h=list(d.values())
count=0
for i in range(len(e)) :
  for j in range((len(g))):
    if e[i]==g[j]:
      count+=f[i]*h[j]*2**(N-2)
count=count/2

print(2**N-1-count)

Submission status: Runtime Error

Solution code:
MAX_A = 10**18+1
DELIM = 1000000007

def kaijo(N):
    if(N==0):
        return 1
    if(N%2==1):
        return (2 * ((kaijo(2, N//2) ** 2)%DELIM)) % DELIM
    else:
        return ((kaijo(2, N//2) % DELIM) ** 2) % DELIM

def main():
    N = int(input())
    AB = [[0] * 2 for i in range(N)]
    X = [0] * N
    no_count_comb = 0
    A_Zeros = []
    B_Zeros = []
    X_Dict = dict()
    for i in range(N):
        A, B = map(int, input().split()))
        if(A == 0):
            A_Zeros.append(i)
            no_count += len(B_Zeros)
        if(B == 0):
            B_Zeros.append(i)
            no_count += len(A_Zeros)
            X[i] = MAX_A

        else:
            X = A / B
            if(X in X_Dict):
                X_Dict[X] += 1
            else:
                X_Dict[X] = 1

            if(1 / X in X_Dict):
                no_count_comb += X_Dict[1/X]
        

    total = kaijo(N) - 1
    no_count = kaijo(N-2)
    print((total - no_count_comb * no_count_comb)%DELIM)

main()

Submission status: Runtime Error

Solution code:
N = int(input())
AB = []
A0 = 0
B0 = 0
for i in range(N):
    Atmp,Btmp = list(map(int,input().split()))
    if (Atmp!=0)and(Btmp!=0):
        AB.append(Atmp/Btmp)
    elif (Atmp==0)and(Btmp==0):
        A0B0 += 1
    elif (Atmp==0)and(Btmp!=0):
        A0 += 1
    elif (Atmp!=0)and(Btmp==0):
        B0 += 1

N += -1*A0B0

def power_func(a,n,p):
  bi=str(format(n,"b"))#2進表現に
  res=1
  for i in range(len(bi)):
    res=(res*res) %p
    if bi[i]=="1":
      res=(res*a) %p
  return res

from collections import Counter
import math
C = Counter(AB)

kaburi = []
forsum = []
for key in C:
    if key<0:
        if C[-1/key]!=0:
            kaburi.append([C[key],C[-1/key]])
            forsum.append(C[key]+C[-1/key])

kaburi.append([A0,B0])
forsum.append(A0+B0)

p = 10**9+7

free = N-sum(forsum)
free2 = power_func(2,free,p)
L = len(kaburi)


out = 1
for i in range(len(kaburi)):
    X = kaburi[i][0]
    Y = kaburi[i][1]
    B = power_func(2,X,p)
    C = power_func(2,Y,p)
    out = out*((B+C-1)%p)
    out = out%p
print((out*free2-1)%p)

Submission status: Runtime Error

Solution code:
N = int(input())
abp = []
abm = []
abz = []
bad_naka = []
for i in range(N):
    q,qq = map(int,input().rstrip().split(" "))
    if q == 0 or qq == 0:
        abz.append(0)
    elif q / qq < 0:
        abm.append(q / qq)
    else:
        abp.append(q / qq)

abm.sort()

abp.sort()

c_p = 0
c_m = 0

bnc = 1
bnc_2 = 1

ans = 0

nokori = N

while c_p != len(abp) -1 or c_m != len(abm) -1:
    if abp[c_p] * abm[c_m] == -1:
        if c_p + 1 <= len(abp) - 1 and abs(abp[c_p + 1]) == abs(abp[c_p]):
            c_p += 1
            bnc += 1
        elif c_m + 1 <= len(abm) -1 and abs(abm[c_m + 1]) == abs(abm[c_m]):
            c_m += 1
            bnc_2 += 1
        else:
            bad_naka.append([bnc,bnc_2])
            if c_m >= len(abm) - 1:
                c_p += 1
            else:
                c_m += 1
            bnc = 1
            bnc_2 = 1

    elif abs(abp[c_p]) * abs(abm[c_m]) < 1 or c_m >= len(abm) - 1:
        c_p += 1
    else:
        c_m += 1

ans += len(abz)
nokori -= len(abz)
for i in range(len(bad_naka)):
    by = (bad_naka[i][1] * (bad_naka[i][1] + 1)) / 2 + (bad_naka[i][0] * (bad_naka[i][0] + 1)) / 2
    nokori -= bad_naka[i][1] + bad_naka[i][0]
    ans *= by + 1
    ans += by
    ans %= 10 ** 10 + 7
for i in range(nokori):
    ans *= 2
    ans += 1
    ans %= 10 ** 10 + 7
    
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from math import gcd
from collections import Counter
# # def input(): return sys.stdin.readline().rstrip()
# # input = sys.stdin.readline
input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10 ** 7)
MOD = 1000000007

N = int(input())
ab = []
for i in range(N):
    a,b = map(int, input().split())
    gcd_ab = gcd(a,b)
    a //= gcd_ab
    b //= gcd_ab
    if a<0:     # aは常に正にする
        a *= -1
        b *= -1
    ab.append((a,b))
Cnt = Counter(ab)
# print(Cnt)

ans = 1
done = set()
for (a, b), c in Cnt.items():
    # print(a,b,c)
    if (a,b) in done: continue
    if b < 0:
        if (-b, a) in Cnt.keys():
            ans *= ((pow(2,c,MOD)-1) + (pow(2,Cnt[(-b,a)],MOD)-1) + 1) + MOD
            ans %= MOD
            done.add((a,b))
            done.add((-b,a))
        else:
            ans *= pow(2,c,MOD)
            ans %= MOD
            done.add((a,b))
    else:
        if (b, -a) in Cnt.keys():
            ans *= ((pow(2,c,MOD)-1) + (pow(2,Cnt[(b,-a)],MOD)-1) + 1) + MOD
            ans %= MOD
            done.add((a,b))
            done.add((b,-a))
        else:
            ans *= pow(2,c,MOD)
            ans %= MOD
            done.add((a,b))
# print(done)
print(ans-1)

Submission status: Runtime Error

Solution code:
from collections import defaultdict as dd
MOD = 10**9+7
N=int(input())
ab = [tuple(map(int, input().split())) for i in range(N)]
res = 0
left_dic = dd(int)
center_dic = dd(int)
right_dic = dd(int)
for a,b in ab:
    if a==b or -a==b:
        center_dic[a/b] += 1
    elif abs(a)>abs(b):
        left_dic[a/b] += 1
    elif abs(a)<abs(b):
        right_dic[b/a] += 1
comp = []
# center
if center_dic[1.0]>0 and center_dic[-1.0]>0:
    comp.append((center_dic[1.0],center_dic[-1.0]))
# print(left_dic)
# print(right_dic)
# left,right
left = sorted(left_dic.keys(),reverse=True)
right = sorted(right_dic.keys())
idx_left = 0
idx_right = 0
S = len(left)
T = len(right)
# left:+, right:-
while 1:
    if idx_left>=S or idx_right>=T:
        break
    l = left[idx_left]
    r = -right[idx_right]
    if l<0 or r<0:
        break
    if l==r:
        comp.append((left_dic[l], right_dic[-r]))
        idx_left += 1
        idx_right += 1
    elif l>r:
        idx_left += 1
    else:
        idx_right += 1
if idx_right<N and idx_left<N:
    left = [-val for val in left if val < 0]
    # left = left[::-1]
    right = [val for val in right if val > 0]
    S = len(left)
    T = len(right)
    idx_left = 0
    idx_right = 0
    while 1:
        if idx_left >= S or idx_right >= T:
            break
        l = left[idx_left]
        r = right[idx_right]
        if l < 0 or r < 0:
            break
        if l == r:
            comp.append((left_dic[-l], right_dic[r]))
            idx_right += 1
            idx_left += 1
        elif l < r:
            idx_left += 1
        else:
            idx_right += 1
res2 = 0
rest = N
buff = 1
for s,t in comp:
    rest -= s+t
    # tmp = s*t
    tmp = (2**(s+t)-2**s-2**t+1)%MOD
    res2 = (res2 + buff*tmp*pow(2,rest,MOD))%MOD
    buff = (2**(s+t)-tmp)%MOD
res2 += 1
res = (pow(2,N,MOD)-res2)%MOD
print(res)

Submission status: Runtime Error

Solution code:
N=int(input())

a_list=[]
b_list=[]
for i in range(N) :
  a,b=map(int,input().split())

  a_list.append(-a/b)
  b_list.append(b/a)

import collections
c=collections.Counter(a_list)
d=collections.Counter(b_list)
e=list(c.keys())
f=list(c.values())
g=list(d.keys())
h=list(d.values())
count=0
for i in range(len(e)) :
  for j in range((len(g))):
    if e[i]==g[j]:
      count+=f[i]*h[j]*2**(N-2)
count=count/2
print(count)
print(2**N-1-count)