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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ii = pair<ll, ll>;

const ll MOD{1000000007};

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll fast_exp(ll a, ll n) {
  ll res = 1, base = a;

  while (n) {
    if (n & 1)
      res = (res * base) % MOD;

    base = (base * base) % MOD;
    n >>= 1;
  }

  return res;
}

ll solve(int N, const vector<ii> &xs) {
  map<ii, vector<int>> ab, ba;
  set<int> ys;
  int zeroes = 0;

  for (int i = 0; i < N; ++i) {
    auto a = xs[i].first, b = xs[i].second;

    if (a == 0 and b == 0) {
      ++zeroes;
      continue;
    }

    auto sa = a < 0 ? -1 : 1;
    auto sb = b < 0 ? -1 : 1;
    auto signal = sa * sb;

    a = llabs(a);
    b = llabs(b);

    auto d = gcd(a, b);
    a /= d;
    b /= d;

    ab[ii(signal * a, b)].push_back(i);
    ba[ii(-signal * b, a)].push_back(i);
    ys.insert(i);
  }

  ll ans = 1;

  for (auto p : ab) {
    if (ys.count(p.second.back()) == 0)
      continue;

    auto it = ba.find(p.first);

    if (it == ba.end()) {
      ans = (ans * fast_exp(2, p.second.size())) % MOD;

      for (auto x : p.second)
        ys.erase(x);
    } else {
      ll r = fast_exp(2, p.second.size());
      ll s = fast_exp(2, it->second.size());
      ll t = ((((r + s - 1) % MOD) + MOD) % MOD);
      ans = (ans * t) % MOD;

      for (auto x : p.second)
        ys.erase(x);

      for (auto x : it->second)
        ys.erase(x);
    }
  }

  ans = ((((ans - 1) % MOD) + MOD) % MOD);

  if (zeroes)
    ans = (ans + zeroes) % MOD;

  return ans;
}

int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<ii> xs(N);

  for (int i = 0; i < N; ++i)
    cin >> xs[i].first >> xs[i].second;

  auto ans = solve(N, xs);

  cout << ans << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ii = pair<ll, ll>;

const int MAX{200010};
const ll MOD{1000000007};

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll fast_exp(ll a, ll n) {
  ll res = 1, base = a;

  while (n) {
    if (n & 1)
      res = (res * base) % MOD;

    base = (base * base) % MOD;
    n >>= 1;
  }

  return res;
}

ll solve(int N, const vector<ii> &xs) {
  map<ii, vector<int>> ab, ba;
  set<int> ys;

  for (int i = 0; i < N; ++i) {
    auto a = xs[i].first, b = xs[i].second;

    auto sa = a < 0 ? -1 : 1;
    auto sb = b < 0 ? -1 : 1;
    auto signal = sa * sb;

    a = llabs(a);
    b = llabs(b);

    auto d = gcd(a, b);
    a /= d;
    b /= d;

    ab[ii(signal * a, b)].push_back(i);
    ba[ii(-signal * b, a)].push_back(i);
    ys.insert(i);
  }

#ifdef DEBUG
  cout << "as = " << endl;

  for (auto p : ab) {
    cout << "-- (" << p.first.first << ", " << p.first.second << ") = ";
    for (auto x : p.second)
      cout << x << ' ';
    cout << endl;
  }

  cout << "bs = " << endl;

  for (auto p : ba) {
    cout << "-- (" << p.first.first << ", " << p.first.second << ") = ";
    for (auto x : p.second)
      cout << x << ' ';
    cout << endl;
  }
#endif

  ll ans = 1;

  for (auto p : ab) {
    if (ys.count(p.second.back()) == 0) {
#ifdef DEBUG
      cout << "== " << p.second.back() << " já processado" << endl;
#endif
      continue;
    }

    auto it = ba.find(p.first);
#ifdef DEBUG
    cout << "-- (" << p.first.first << ", " << p.first.second << ") = ";
    for (auto x : p.second)
      cout << x << ' ';
    cout << endl;

    cout << "   it = ";

    if (it == ba.end())
      cout << "-" << endl;
    else {
      for (auto x : it->second)
        cout << x << ' ';
      cout << endl;
    }
#endif

    if (it == ba.end()) {
      ans = (ans * fast_exp(2, p.second.size())) % MOD;

      for (auto x : p.second)
        ys.erase(x);
    } else {
      ll r = fast_exp(2, p.second.size());
      ll s = fast_exp(2, it->second.size());
      ll t = ((((r + s - 1) % MOD) + MOD) % MOD);
      ans = (ans * t) % MOD;

#ifdef DEBUG
      cout << "+++ r = " << r << ", s = " << s << ", t = " << t << endl;
#endif
      for (auto x : p.second)
        ys.erase(x);

      for (auto x : it->second)
        ys.erase(x);
    }

#ifdef DEBUG
    cout << "   new ans = " << ans << endl;
#endif
  }

  return ((((ans - 1) % MOD) + MOD) % MOD);
}

int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<ii> xs(N);

  for (int i = 0; i < N; ++i)
    cin >> xs[i].first >> xs[i].second;

  auto ans = solve(N, xs);

  cout << ans << '\\n';

  return 0;
}