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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int mod = 1e9 + 7;

template <typename T> ostream &operator<<(ostream &os, const pair<T, T> p) {
  os << "<" << p.first << ", " << p.second << ">";
  return os;
}

template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll modpow(ll n, int k) {
  ll r = 1;
  while (k > 0) {
    if (k & 1)
      (r *= n) %= mod;
    (n *= n) %= mod;
    k >>= 1;
  }
  return r;
}

template <typename T> pair<T, T> flip(const pair<T, T> &p) {
  T a = p.first;
  T b = p.second;

  assert(b >= 0);

  if (a < 0) {
    return {b, -a};
  } else {
    return {-b, a};
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  map<pair<ll, ll>, ll> cnt;

  int n;
  cin >> n;

  ll res0 = 0;
  ll res = 1;

  for (int i = 0; i < n; ++i) {
    ll a, b;
    cin >> a >> b;

    if (a == 0 && b == 0) {
      ++res0;
      continue;
    }

    if (b < 0) {
      a = -a;
      b = -b;
    }

    ll g = gcd(a, b);
    a /= g;
    b /= g;

    if (b < 0) {
      a = -a;
      b = -b;
    }

    // cout << a << " " << b << "\\n";

    ++cnt[{a, b}];
  }

  for (const auto &elem : cnt) {
    ll a = elem.first.first;
    ll b = elem.first.second;

    // cout << "pair: " << elem.first << "\\n";
    auto fl = flip(elem.first);
    // cout << "flipped pair " << fl << "\\n";

    map<pair<ll, ll>, ll>::iterator bad;
    if (a == 0 || b == 0) {
      bad = cnt.find({b, a});
    } else {
      bad = cnt.find(fl);
    }

    ll amt = modpow(2, elem.second);

    if (bad != cnt.end()) {
      // cout << "found it\\n";
      amt = amt + (modpow(2, (*bad).second)) + mod - 1;
      cnt.erase(bad);
    } else {
      // cout << "didn't find it\\n";
    }

    // cout << "amt: " << amt << "\\n";
    res = (res * amt) % mod;
    // cout << string(100, '-') << "\\n";
  }

  ll ans = (res + res0 + mod - 1) % mod;
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int mod = 1e9 + 7;

ostream &operator<<(ostream &os, const pair<int, int> p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll modpow(ll n, int k) {
  ll r = 1;
  while (k > 0) {
    if (k & 1)
      (r *= n) %= mod;
    (n *= n) %= mod;
    k >>= 1;
  }
  return r;
}

pair<int, int> flip(const pair<int, int> &p) {
  int a = p.first;
  int b = p.second;

  assert(b >= 0);

  if (a < 0) {
    return {b, -a};
  } else {
    return {-b, a};
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  map<pair<ll, ll>, ll> cnt;

  int n;
  cin >> n;

  ll res0 = 0;
  ll res = 1;

  for (int i = 0; i < n; ++i) {
    ll a, b;
    cin >> a >> b;

    if (a == 0 && b == 0) {
      ++res0;
      continue;
    }

    // if (b < 0) {
    // a = -a;
    // b = -b;
    //}

    ll g = gcd(a, b);
    a /= g;
    b /= g;

    if (b < 0) {
      a = -a;
      b = -b;
    }

    // cout << a << " " << b << "\\n";

    ++cnt[{a, b}];
  }

  for (const auto &elem : cnt) {
    ll a = elem.first.first;
    ll b = elem.first.second;

    // cout << "pair: " << elem.first << "\\n";
    auto fl = flip(elem.first);
    // cout << "flipped pair " << fl << "\\n";

    map<pair<ll, ll>, ll>::iterator bad;
    if (a == 0 || b == 0) {
      bad = cnt.find({b, a});
    } else {
      bad = cnt.find(fl);
    }

    ll amt = modpow(2, elem.second);

    if (bad != cnt.end()) {
      // cout << "found it\\n";
      amt = amt + (modpow(2, (*bad).second)) + mod - 1;
      cnt.erase(bad);
    } else {
      // cout << "didn't find it\\n";
    }

    // cout << "amt: " << amt << "\\n";
    res = (res * amt) % mod;
    // cout << string(100, '-') << "\\n";
  }

  ll ans = (res + res0 + mod - 1) % mod;
  cout << ans << "\\n";
  return 0;
}