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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 2e5 + 10;
const ll MOD = 1e9 + 7;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
int N;
ll A[MAXN];
ll B[MAXN];
map<pair<ll, ll>, int> cnt;
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void calfact() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = factor[u - 1] * u % MOD;
  }
}
ll C(int n, int m) {
  assert(n >= m);
  return factor[n] * qpow(factor[m], MOD - 2) * qpow(factor[n - m], MOD - 2);
}
ll collect(ll a, ll b) {
  ll rt = 0;
  rt += cnt[{a, b}];
  rt += cnt[{-a, -b}];
  return rt;
}
void erase(ll a, ll b) {
  cnt[{a, b}] = 0;
  cnt[{-a, -b}] = 0;
}
int zero = 0;
void read() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  calfact();
  cin >> N;
  for (int u = 1; u <= N; u++) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      zero++;
      continue;
    }
    ll g = gcd(a, b);
    assert(g != 0);
    cnt[{a / g, b / g}]++;
    A[u] = a / g;
    B[u] = b / g;
  }
}
void solve() {
  read();
  ll ans = 1;
  for (int u = 1; u <= N; u++) {
    if (cnt[{A[u], B[u]}] == 0)
      continue;
    ll t1 = collect(A[u], B[u]), t2 = collect(-B[u], A[u]);
    ans = ans * ((qpow(2, t1) + qpow(2, t2) - 1) % MOD) % MOD;
    erase(A[u], B[u]);
    erase(-B[u], A[u]);
  }
  ans = (ans + zero - 1 + MOD) % MOD;
  cout << ans;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 2e5 + 10;
const ll MOD = 1e9 + 7;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
int N;
ll A[MAXN];
ll B[MAXN];
map<pair<ll, ll>, int> cnt;
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void calfact() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = factor[u - 1] * u % MOD;
  }
}
ll C(int n, int m) {
  assert(n >= m);
  return factor[n] * qpow(factor[m], MOD - 2) * qpow(factor[n - m], MOD - 2);
}
ll collect(ll a, ll b) {
  ll rt = 0;
  if (cnt.count({a, b}))
    rt += cnt[{a, b}];
  if (cnt.count({-a, -b}))
    rt += cnt[{-a, -b}];
  return rt;
}
ll erase(ll a, ll b) {
  auto p1 = cnt.find({a, b}), p2 = cnt.find({-a, -b});
  if (p1 != cnt.end())
    cnt.erase(p1);
  if (p2 != cnt.end())
    cnt.erase(p2);
}
int zero = 0;
void read() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  calfact();
  cin >> N;
  for (int u = 1; u <= N; u++) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      zero++;
      continue;
    }
    ll g = gcd(a, b);
    cnt[{a / g, b / g}]++;
    A[u] = a / g;
    B[u] = b / g;
  }
}
void solve() {
  read();
  ll ans = 1;
  for (int u = 1; u <= N; u++) {
    if (cnt.count({A[u], B[u]}) == 0)
      continue;
    ll t1 = collect(A[u], B[u]), t2 = collect(-B[u], A[u]);
    ans = ans * (qpow(2, t1) + qpow(2, t2) - 1) % MOD;
    erase(A[u], B[u]);
    erase(-B[u], A[u]);
  }
  ans = (ans + zero - 1) % MOD;
  cout << ans;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}