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)

//package abc168

import java.io.*
import java.math.*
import java.util.*
import kotlin.math.*

var reader: BufferedReader = System.`in`.bufferedReader()
var writer: PrintWriter = PrintWriter(System.`out`)

fun next() = reader.readLine()!!
fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextDouble() = next().toDouble()
fun nextStrings() = next().split(" ")
fun nextInts() = nextStrings().map { it.toInt() }
fun nextLongs() = nextStrings().map { it.toLong() }
fun nextDoubles() = nextStrings().map { it.toDouble() }

fun put(v: Any?) = writer.print(v)
fun puts() = writer.println()
fun puts(v: Any?) = writer.println(v)

var debug = false
fun dprintln(v: Any?) {
    if (debug) println(v)
}
fun dprintln() {
    if (debug) println()
}

class ModInt(
    val value: Long
) {
    private val POW = mutableMapOf<Int, ModInt>()

    constructor (value: Int): this(value.toLong())

    operator fun plus(rhs: ModInt): ModInt {
        return ModInt((value + rhs.value) % M)
    }
    operator fun minus(rhs: ModInt): ModInt {
        return ModInt((value - rhs.value + M) % M)
    }
    operator fun times(rhs: ModInt): ModInt {
        return ModInt((value * rhs.value) % M)
    }
    operator fun div(rhs: ModInt): ModInt {
        return ModInt(value * inv(rhs.value) % M)
    }
    private fun inv(v: Long): Long {
        return INV.computeIfAbsent(v) {
            M - (((M / v) * inv(M % v)) % M)
        }
    }
    fun pow(v: Int): ModInt {
        return POW.computeIfAbsent(v) {
            var result = 1L
            var x = v
            var vv = value
            while (x != 0) {
                if ((x and 1) != 0) {
                    result = (result * vv) % M
                }
                vv = (vv * vv) % M
                x = x ushr 1
            }
            ModInt(result)
        }
    }

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

    companion object {
        private const val M = 1000000007L

        private val INV = mutableMapOf<Long, Long>().also {
            it[1] = 1
        }

        val ZERO = ModInt(0)
        val ONE = ModInt(1)
    }
}

fun test(case: Int, input: String, expected: String) {
    reader = input.trimIndent().reader().buffered()

    val stringWriter = StringWriter()
    writer = PrintWriter(stringWriter)

    solve()
    writer.flush()

    val actual = stringWriter.toString()

    println("[test $case]")
    if ("${expected}" == actual) {
        println("OK")
    } else {
        println("NG")
        println()
        println("expected: \"${expected}\"")
        println("actual:   \"${actual}\"")
    }
    println()
}

fun main(args: Array<String>) {
    if ((args.size == 1) && (args[0] == "test")) {
        testAll()
    } else {
        solve()
        writer.flush()
    }
}

///////////////////////////////////////////////////////////

fun testAll() {
    debug = true

    test(1,
        """
        3
        1 2
        -1 1
        2 -1
        """,
        "5"
    )
    test(2,
        """
        10
        3 2
        3 2
        -1 1
        2 -1
        -3 -9
        -8 12
        7 7
        8 1
        8 2
        8 4
        """,
        "479"
        )
        test(3,
        """
        4
        0 3
        0 -3
        1 0
        -1 0
        """,
        "6"
        )
}


val TWO = ModInt(2)

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

fun solve() {

    val TWO = ModInt(2)
    val N = nextInt()
    val fishes = mutableMapOf<Pair<Long, Long>, Int>()
    var numZero = 0

    repeat(N) {
        var (a, b) = nextLongs()

        if ((a == 0L) && (b == 0L)) {
            ++numZero
        } else if (a == 0L) {
            fishes.merge(Pair(0L, 1L), 1) { x, y -> x + y }
        } else if (b == 0L) {
            fishes.merge(Pair(-1L, 0L), 1) { x, y -> x + y }
        } else {
            val g = gcd(a, b)
            a /= g
            b /= g

            fishes.merge(Pair(a, b), 1) { x, y -> x + y }
        }
    }
    dprintln(fishes)

    val result = fishes.entries.fold(ModInt(1)) { acc, (fish, c) ->

        if (fishes.containsKey(Pair(fish.second, -fish.first))) {
            return@fold acc
        }

        val oc = fishes.getOrDefault(Pair(-fish.second, fish.first), 0)

        acc * (TWO.pow(c) + TWO.pow(oc) - ModInt.ONE)
    }
    dprintln(result)

    put(result + ModInt(numZero) - ModInt.ONE)
}