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 in Fortran (GNU Fortran 9.2.1) (judge verdict: Wrong Answer)

program abc168e
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i
    integer(int64):: a_zero,b_zero,cp,cm,czz,add
    integer(int64):: nakawaru
    integer(int64):: pi,mi,g
    integer(int64):: dp,dm,ans
    integer(int64), parameter:: md=1000000007
    integer(int64), allocatable:: a(:),b(:)
    integer(int64), allocatable:: pa(:),pb(:)
    integer(int64), allocatable:: ma(:),mb(:)


    read*, n
    allocate(a(n),b(n))
    cp=0
    cm=0
    b_zero=0
    a_zero=0
    czz=0
    do i=1,n
        read*, a(i),b(i)
    end do

    do i=1,n
        if (a(i)==0 .and. b(i)==0)then
            ! count zero_zero
            czz=czz+1
        else if (a(i) == 0) then
            ! count zero_nonzero
            a_zero=a_zero+1
        else if (b(i) == 0) then
            ! count nonzero_zero
            b_zero=b_zero+1
        else if (a(i) > 0 .eqv. b(i) > 0) then
            ! count a,b >0 , a,b <0
            cp=cp+1
        else
            cm=cm+1
        end if
    end do
    ! count a>0>b b>0>a
    allocate(pa(cp),pb(cp))
    allocate(ma(cm),mb(cm))
    pi=1
    mi=1
    ans = mod((pow2(a_zero)+pow2(b_zero)-1),md)
    
    do i=1,n
        if (a(i)==0 .or. b(i)==0) cycle
        ! nonzero a,b
        g = abs(gcd(a(i),b(i)))
        if (a(i) > 0 .eqv. b(i) > 0) then
            pa(pi)=a(i)/g
            pb(pi)=b(i)/g
            pi=pi+1
        else
            ma(mi)=abs(a(i))/g
            mb(mi)=abs(b(i))/g
            mi=mi+1
        end if
    end do

    if (cp > 0) then
        call double_sort(pb,pa,1_8,cp)
        call double_sort(pa,pb,1_8,cp)
    end if

    if (cm > 0) then
        call double_sort(ma,mb,1_8,cm)
        call double_sort(mb,ma,1_8,cm)
    end if

    pi=1
    mi=1
    do while(pi <= cp .and. mi <= cm)
        if (pa(pi) > mb(mi)) then
            ans=mod(ans*2_8,md) 
            mi=mi+1
        else if (pa(pi) < mb(mi)) then
            ans=mod(ans*2_8,md)
            pi=pi+1
        else
            if (pb(pi) > ma(mi)) then
                ans=mod(ans*2_8,md)
                mi=mi+1
            else if (pb(pi) < ma(mi)) then
                ans=mod(ans*2_8,md)
                pi=pi+1
            else
                dp=0
                dm=0
                do while(pb(pi)==pb(pi+dp) .and. pa(pi)==pa(pi+dp))
                    dp=dp+1
                    if (pi+dp > cp) exit
                end do
                do while(ma(mi)==ma(mi+dm) .and. mb(mi)==mb(mi+dm))
                    dm=dm+1
                    if (mi+dm > cm) exit
                end do
                nakawaru = mod(pow2(dp)+pow2(dm)-1,md)
                ans=mod(ans*nakawaru,md)
                pi=pi+dp
                mi=mi+dm
            end if
        end if
    end do

    if (pi <= cp) then
        add = pow2(cp-pi+1)
        ans = mod(ans*add,md)
    else
        add = pow2(cm-mi+1)
        ans = mod(ans*add,md)
    end if
    print'(i0)', mod(ans+czz-1,md)
contains
    function pow2(x) result(ret)
        integer(int64):: x,ret
        ret = factorial(2_8,x,md)
    end function
    function factorial(a,b,md) result(ret)
        integer(int64):: a,b,md
        integer(int64):: aa,bb,ret
        aa = a
        bb = b
        ret = 1
        do while(bb > 0)
            if (mod(bb,2_8)/=0) ret=mod(ret*aa,md)
            aa=mod(aa*aa,md)
            bb=bb/2
        end do
    end function


    recursive function gcd(x,y) result(ret)
        integer(int64):: x,y,ret

        if (mod(x,y) == 0) then
            ret = y
            return
        end if

        ret = gcd(y,mod(x,y))
    end function

    recursive subroutine double_sort(ar1, ar2, fst, lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap(ar1(fst), ar1(lst))
                call swap(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call double_sort(ar1,ar2,fst,mdl)
        call double_sort(ar1,ar2,mdl+1,lst)
        call double_merge_(ar1,ar2,fst,mdl,lst)
    end subroutine


    subroutine double_merge_(ar1,ar2,fst,mdl,lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,mdl,lst
        integer(int64),allocatable:: t1(:),t2(:)
        integer(int64):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li) 
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    
    subroutine swap(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine
end program abc168e