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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;

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

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N;
  cin >> N;
  map<pair<ll, ll>, ll> X;
  ll A0 = 0, B0 = 0, AB0 = 0;
  REP(i, N) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0)
      AB0++;
    else if (a == 0)
      A0++;
    else if (b == 0)
      B0++;
    else {
      ll g = gcd(abs(a), abs(b));
      ll aa = a / g;
      ll bb = b / g;
      pair<ll, ll> p1;
      if (bb < 0) {
        p1 = make_pair(-aa, -bb);
      } else {
        p1 = make_pair(aa, bb);
      }

      X[p1]++;
    }
  }

  ll ans = 1;
  X[make_pair(-1, 0)] = A0;
  X[make_pair(0, 1)] = B0;
  for (auto &&p : X) {
    pair<ll, ll> ab = p.first;
    ll a = ab.first;
    ll b = ab.second;
    ll num = p.second;
    if (num == 0)
      continue;

    ll t;
    if (a < 0) {
      t = X[make_pair(b, -a)];
      X[make_pair(b, -a)] = 0;
    } else {
      t = X[make_pair(-b, a)];
      X[make_pair(-b, a)] = 0;
    }

    ll y1 = mod_pow(2, num, MOD);
    ll y2 = mod_pow(2, t, MOD);
    y1 %= MOD;
    y2 %= MOD;
    ans *= (y1 + y2 - 1);
    ans %= MOD;
  }

  ans += MOD - 1 + AB0;
  ans %= MOD;

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;

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

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N;
  cin >> N;
  map<pair<ll, ll>, ll> X;
  ll A0 = 0, B0 = 0, AB0 = 0;
  REP(i, N) {
    ll a, b;
    cin >> a >> b;
    ll g = gcd(abs(a), abs(b));
    if (g == 0) {
      if (a == 0 && b == 0)
        AB0++;
      else if (a == 0)
        A0++;
      else
        B0++;
    }

    ll aa = a / g;
    ll bb = b / g;
    pair<ll, ll> p1;
    if (aa < 0 && bb < 0) {
      p1 = make_pair(-aa, -bb);
    } else if (bb < 0) {
      p1 = make_pair(-aa, -bb);
    } else if (aa < 0) {
      p1 = make_pair(aa, bb);
    } else {
      p1 = make_pair(aa, bb);
    }

    X[p1]++;
  }

  ll ans = 1;
  for (auto &&p : X) {
    pair<ll, ll> ab = p.first;
    ll a = ab.first;
    ll b = ab.second;
    ll num = p.second;
    if (num == 0)
      continue;

    ll t;
    if (a < 0) {
      t = X[make_pair(b, -a)];
      X[make_pair(b, -a)] = 0;
    } else {
      t = X[make_pair(-b, a)];
      X[make_pair(-b, a)] = 0;
    }

    if (t == 0) {
      ans *= 2;
    } else {
      ll y1 = mod_pow(2, num, MOD);
      ll y2 = mod_pow(2, t, MOD);
      ans *= (y1 + y2 - 1);
    }
    ans %= MOD;
  }

  ans *= (mod_pow(2, A0, MOD) + mod_pow(2, B0, MOD) - 1);
  ans %= MOD;

  ans *= (AB0 + 1);
  ans %= MOD;

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

  cout << ans << endl;
  return 0;
}