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 Rust (1.42.0) (judge verdict: Accepted)

use proconio::{fastout, input};
// use proconio::marker::*;
use std::collections::BTreeMap;

fn gcd(a: i64, b: i64) -> i64 {
   if a == 0 {
      b
   } else if b == 0 {
      a
   } else {
      gcd(b, a % b)
   }
}

#[fastout]
fn main() {
   input! {
      mut n: usize,
      ab: [(i64, i64); n]
   }
   let mut zero = 0;
   let mut hm = BTreeMap::<(i64, i64), (usize, usize)>::new();
   for (_a, _b) in ab {
      if _a == 0 && _b == 0 {
         zero += 1;
         continue;
      }

      let m = gcd(_a.abs(), _b.abs());
      let a = _a / m;
      let b = _b / m;

      if a == 0 {
         hm.entry((0, 1))
            .and_modify(|(l, _)| *l += 1)
            .or_insert((1, 0));
      } else if b == 0 {
         hm.entry((0, 1))
            .and_modify(|(_, r)| *r += 1)
            .or_insert((0, 1));
      } else if a.is_negative() ^ b.is_negative() {
         hm.entry((b.abs(), a.abs()))
            .and_modify(|(_, r)| *r += 1)
            .or_insert((0, 1));
      } else {
         hm.entry((a.abs(), b.abs()))
            .and_modify(|(l, _)| *l += 1)
            .or_insert((1, 0));
      }
   }

   let mut ans = Mint::new(1);
   for &(l, r) in hm.values() {
      ans *= Mint::new(2).pow(l) + Mint::new(2).pow(r) - Mint::new(1);
   }
   ans -= Mint::new(1);
   ans += Mint::new(zero);
   println!("{}", ans);
}

use std::ops::*;
const MOD: i64 = 1000000007;
#[derive(Copy, Clone, Debug)]
pub struct Mint {
   x: i64,
}
impl Mint {
   fn new(x: i64) -> Self { Mint { x: x } }
   fn pow(self, n: usize) -> Self {
      let mut x = Mint::new(1);
      let mut r = self;
      let mut n = n;
      while n > 0 {
         if n & 1 == 1 {
            x *= r;
         }
         r *= r;
         n >>= 1;
      }
      x
   }
   fn inv(self) -> Self { self.pow((MOD - 2) as usize) }
}
impl AddAssign for Mint {
   fn add_assign(&mut self, rhs: Self) {
      self.x += rhs.x;
      self.x %= MOD;
   }
}
impl Add for Mint {
   type Output = Self;
   fn add(self, other: Self) -> Self {
      let mut res = self;
      res += other;
      res
   }
}
impl SubAssign for Mint {
   fn sub_assign(&mut self, rhs: Self) {
      self.x -= rhs.x;
      if self.x < 0 {
         self.x += MOD;
      }
   }
}
impl Sub for Mint {
   type Output = Self;
   fn sub(self, other: Self) -> Self {
      let mut res = self;
      res -= other;
      res
   }
}
impl MulAssign for Mint {
   fn mul_assign(&mut self, rhs: Self) {
      self.x *= rhs.x;
      self.x %= MOD;
   }
}
impl Mul for Mint {
   type Output = Self;
   fn mul(self, other: Self) -> Self {
      let mut res = self;
      res *= other;
      res
   }
}
impl DivAssign for Mint {
   fn div_assign(&mut self, rhs: Self) { *self *= rhs.inv(); }
}
impl Div for Mint {
   type Output = Self;
   fn div(self, other: Self) -> Self {
      let mut res = self;
      res *= other;
      res
   }
}
impl From<i64> for Mint {
   fn from(num: i64) -> Self { Mint { x: num } }
}
impl From<i32> for Mint {
   fn from(num: i32) -> Self { Mint { x: num as i64 } }
}
impl From<usize> for Mint {
   fn from(num: usize) -> Self { Mint { x: num as i64 } }
}
use std::fmt;
impl fmt::Display for Mint {
   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.x) }
}