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 Kotlin (1.3.71) (judge verdict: Accepted)

import java.util.*

fun main(args: Array<String>) {
    val N = readLine()!!.toInt()
    val map = HashMap<V, MutablePair<Long, Long>>()
    var zero = 0L
    repeat(N) {
        val (x, y) = readLine()!!.split(" ").map(String::toLong)
        if (x == 0L && y == 0L) {
            zero++
            return@repeat
        }
        var v = V(x, y)
        if (v.y < 0 || (v.y == 0L && v.x < 0)) {
            v = v.rot180()
        }
        val rot = v.x <= 0
        if (rot) {
            v = v.rot270()
        }
        v = v.gcd_normalized()
        val g = map.getOrPut(v) { MutablePair(0L, 0L) }
        if (rot) g.g1++
        else g.g2++
    }
    var ans = MInt(1)
    map.values.forEach { g ->
        var sum = MInt(1)
        sum += MInt(2).pow(g.g1) - 1
        sum += MInt(2).pow(g.g2) - 1
        ans *= sum
    }
    ans -= 1 // answer should select at lease 1 fish, so remove a patten that no fish selected
    ans += zero // fish(0,0) is only valid for a pattern in which only the fish is selected.
    println(ans)
}

data class MutablePair<A, B>(var g1: A, var g2: B)

data class V(val x: Long, val y: Long) {
    fun rot90() = V(-y, x)
    fun rot180() = V(-x, -y)
    fun rot270() = V(y, -x)
    fun gcd_normalized() = V(x / gcd(x, y), y / gcd(x, y))
}

tailrec fun gcd(a: Long, b: Long): Long {
    if (a < b) return gcd(b, a)
    if (b == 0L) return a
    if (a <= 0L) throw IllegalArgumentException()
    return gcd(b, a % b)
}

class MInt {
    private val MOD = 1000000007L
    var v: Long

    constructor(v: Long) {
        this.v = v % MOD
    }

    constructor(v: Int) {
        this.v = v.toLong() % MOD
    }

    operator fun plus(b: MInt): MInt {
        val a = MInt(v)
        a.v += b.v
        if (a.v >= MOD) a.v -= MOD
        return a
    }

    operator fun plus(b: Int) = plus(MInt(b))
    operator fun plus(b: Long) = plus(MInt(b))

    operator fun minus(b: MInt): MInt {
        val a = MInt(v)
        a.v -= b.v
        if (a.v < 0) a.v += MOD
        return a
    }

    operator fun minus(b: Int) = minus(MInt(b))
    operator fun minus(b: Long) = minus(MInt(b))

    operator fun times(b: MInt): MInt {
        val a = MInt(v)
        a.v *= b.v
        a.v %= MOD
        return a
    }

    operator fun div(b: MInt): MInt {
        return MInt(v) * b.inv()
    }

    fun inv(): MInt {
        var a = v
        var b = MOD
        var u = 1L
        var w = 0L
        while (b != 0L) {
            val t = a / b
            a -= t * b
            u -= t * w
            var tmp = a
            a = b
            b = tmp
            tmp = u
            u = w
            w = tmp
        }
        u %= MOD
        if (u < 0) u += MOD
        return MInt(u)
    }

    fun pow(N: Int) = pow(N.toLong())

    fun pow(N: Long): MInt {
        var ans = MInt(1)
        var a = this
        var n = N
        while (n > 0) {
            if (n.and(1) == 1L) {
                ans *= a
            }
            a *= a
            n = n.shr(1)
        }
        return ans
    }

    override fun toString(): String {
        return v.toString()
    }
}