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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define ll long long
#define pll pair<ll, ll>

#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

using namespace std;

const int MOD = 1000000007;
const int N = 222222;

ll n;
ll a[N], b[N];

void init() {
  cin >> n;
  rep(i, n) { cin >> a[i] >> b[i]; }
}

ll gcd(ll a, ll b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }
  return a;
}

map<pll, ll> dict;
set<pll> used;

void solve() {
  ll kai[N] = {0};
  kai[0] = 1;
  reps(i, 1, n + 1) { kai[i] = (kai[i - 1] * 2) % MOD; }

  ll zeroCount = 0;

  rep(i, n) {
    ll g = 1;
    if (a[i] != 0 && b[i] != 0) {
      g = gcd(abs(a[i]), abs(b[i]));
    }
    if (a[i] == 0 && b[i] == 0) {
      zeroCount++;
      continue;
    }
    if (a[i] == 0) {
      b[i] = 1;
    }
    if (b[i] == 0) {
      a[i] = 1;
    }

    ll aa = a[i] / g;
    ll bb = b[i] / g;

    if (aa < 0) {
      aa *= -1;
      bb *= -1;
    }

    pll p = pll(aa, bb);
    dict[p]++;
  }

  ll result = 1;

  for (map<pll, ll>::iterator it = dict.begin(); it != dict.end(); it++) {
    pll key = it->first;
    pll rev = pll(key.second, -key.first);
    if (rev.first < 0) {
      rev.first *= -1;
      rev.second *= -1;
    }
    if (rev.first == 0) {
      rev.second = 1;
    }
    if (rev.second == 0) {
      rev.first = 1;
    }

    if (dict.find(rev) != dict.end()) {
      if (used.find(rev) == used.end()) {
        ll acount = it->second;
        ll bcount = dict[rev];

        ll kaiPlus = (kai[acount] + kai[bcount] - 1) % MOD;

        result = (result * kaiPlus) % MOD;
        // cout << "cc " << kai[acount] << " " << kai[bcount] << " " << result
        // << endl;

        used.insert(key);
        used.insert(rev);
      }
    } else {
      result = (result * kai[it->second]) % MOD;

      // cout << "dd " << kai[it->second] << " " << result << endl;
    }
  }

  result = (result + zeroCount) % MOD;

  result -= 1;
  if (result < 0) {
    result += MOD;
  }

  cout << result << endl;
}

int main() {
  init();
  solve();
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define ll long long
#define pll pair<ll, ll>

#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

using namespace std;

const int MOD = 1000000007;
const int N = 222222;

ll n;
ll a[N], b[N];

void init() {
  cin >> n;
  rep(i, n) { cin >> a[i] >> b[i]; }
}

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

map<pll, ll> dict;
set<pll> used;

void solve() {
  ll kai[N] = {0};
  kai[0] = 1;
  reps(i, 1, n + 1) { kai[i] = (kai[i - 1] * 2) % MOD; }

  rep(i, n) {
    ll g = gcd(abs(a[i]), abs(b[i]));

    ll aa = a[i] / g;
    ll bb = b[i] / g;

    if (aa < 0) {
      aa *= -1;
      bb *= -1;
    }

    pll p = pll(aa, bb);
    dict[p]++;
  }

  ll result = 1;

  for (map<pll, ll>::iterator it = dict.begin(); it != dict.end(); it++) {
    pll key = it->first;
    pll rev = pll(key.second, -key.first);
    if (rev.first < 0) {
      rev.first *= -1;
      rev.second *= -1;
    }

    if (dict.find(rev) != dict.end()) {
      if (used.find(rev) == used.end()) {
        ll acount = it->second;
        ll bcount = dict[rev];

        ll kaiPlus = (kai[acount] + kai[bcount] - 1) % MOD;

        result = (result * kaiPlus) % MOD;
        // cout << "cc " << kai[acount] << " " << kai[bcount] << " " << result
        // << endl;

        used.insert(key);
        used.insert(rev);
      }
    } else {
      result = (result * 2) % MOD;

      // cout << "cc2 " << result << endl;
    }
  }

  result -= 1;
  if (result < 0) {
    result += MOD;
  }

  cout << result << endl;
}

int main() {
  init();
  solve();
}