Problem p02679 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

ll rui(ll a, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    n /= 2;
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, n) cin >> a[i] >> b[i];
  map<pii, ll> mp1;
  map<pii, ll> mp2;
  ll N = n;
  ll z = 0;
  ll ans = 1;
  rep(i, N) {
    ll x, y;
    if (a[i] != 0 && b[i] != 0) {
      x = a[i] / gcd(a[i], b[i]);
      y = b[i] / gcd(a[i], b[i]);
    } else if (a[i] == 0 && b[i] == 0) {
      z++;
      n--;
      continue;
    } else if (a[i] == 0) {
      x = 0, y = 1;
    } else {
      x = 1, y = 0;
    }
    mp1[pii(x, y)]++;
    if (x != 0)
      mp2[pii(-y / gcd(y, x), x / gcd(y, x))]++;
    else
      mp2[pii(1, 0)]++;
  }
  for (auto c : mp1) {
    if (c.second == 0 || mp2[c.first] == 0)
      continue;
    ll x = c.first.first;
    ll y = c.first.second;
    ans = ans * (rui(2, c.second) + rui(2, mp2[c.first]) - 1) % mod;
    n -= c.second + mp2[c.first];
    c.second = 0;
    if (x != 0)
      mp1[pii(-y / gcd(y, x), x / gcd(y, x))] = 0;
    else
      mp1[pii(1, 0)] = 0;
  }
  ans = (ans * rui(2, n) - 1) % mod;
  ans = (ans + z) % mod;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

ll rui(ll a, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    n /= 2;
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  rep(i, n) cin >> a[i] >> b[i];
  map<pii, ll> mp1;
  map<pii, ll> mp2;
  ll N = 0;
  ll ans = 1;
  rep(i, n) {
    ll x, y;
    if (b[i] != 0) {
      x = a[i] / gcd(a[i], b[i]);
      y = b[i] / gcd(a[i], b[i]);
    } else if (a[i] != 0) {
      x = 1, y = 0;
    } else {
      N++;
      n--;
      continue;
    }
    mp1[pii(x, y)]++;
    if (x != 0)
      mp2[pii(-y / gcd(y, x), x / gcd(y, x))]++;
    else
      mp2[pii(1, 0)]++;
  }
  for (auto c : mp1) {
    if (c.second == 0 || mp2[c.first] == 0)
      continue;
    ll x = c.first.first;
    ll y = c.first.second;
    ans = ans * (rui(2, c.second) + rui(2, mp2[c.first]) - 1) % mod;
    n -= c.second + mp2[c.first];
    c.second = 0;
    mp1[pii(-y / gcd(y, x), x / gcd(y, x))] = 0;
  }
  ans = (ans * rui(2, n) - 1) % mod;
  ans = (ans + N) % mod;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}