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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

long long gcd(long long N, long long M) {
  if (N == 0 || M == 0) {
    return max(1LL, max(N, M));
  }
  while (true) {
    if (min(N, M) == 0) {
      return max(N, M);
    } else if (N > M) {
      N %= M;
    } else {
      M %= N;
    }
  }
}

int main() {
  int N;
  cin >> N;

  map<pair<ll, ll>, ll> m;
  ll ans = 1;
  for (int i = 0; i < N; i++) {
    ll A, B;
    cin >> A >> B;
    ll g = gcd(abs(A), abs(B));
    A /= g;
    B /= g;
    if (A < 0) {
      A = -A;
      B = -B;
    } else if (A == 0) {
      B = abs(B);
    }
    pair<ll, ll> key = make_pair(A, B);
    if (m.count(key)) {
      m[key]++;
    } else {
      m[key] = 1;
    }
  }

  vector<ll> p2(200001, 1);
  for (int i = 0; i < 200000; i++) {
    p2[i + 1] = 2LL * p2[i];
    p2[i + 1] %= MOD;
  }

  for (auto &kv : m) {
    pair<ll, ll> key = kv.first;
    ll val = kv.second;
    if (key.first == 0 && key.second == 0)
      continue;
    m[key] = 0;
    // cout << key.first << ' ' << key.second << endl;

    pair<ll, ll> inv_key;
    if (key.second <= 0) {
      inv_key = make_pair(-key.second, key.first);
    } else {
      inv_key = make_pair(key.second, -key.first);
    }
    ll inv_val = 0;
    if (m.count(inv_key) && inv_key != key) {
      inv_val = m[inv_key];
      m[inv_key] = 0;
    }

    // cout << inv_key.first << ' ' << inv_key.second << ' ' << val << ' ' <<
    // inv_val << endl;
    ans *= (p2[val] + p2[inv_val] - 1) % MOD;
    ans %= MOD;
  }
  pair<ll, ll> key = make_pair(0, 0);
  if (m.count(key)) {
    ans += m[key];
    ans %= MOD;
  }

  cout << (MOD + ans - 1) % MOD << '\\n';
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

long long gcd(long long N, long long M) {
  if (N == 0 || M == 0) {
    return max(N, M);
  }
  while (true) {
    if (min(N, M) == 0) {
      return max(N, M);
    } else if (N > M) {
      N %= M;
    } else {
      M %= N;
    }
  }
}

int main() {
  int N;
  cin >> N;

  map<pair<ll, ll>, ll> m;
  ll ans = 1;
  for (int i = 0; i < N; i++) {
    ll A, B;
    cin >> A >> B;
    ll g = gcd(abs(A), abs(B));
    A /= g;
    B /= g;
    if (A < 0) {
      A = -A;
      B = -B;
    } else if (A == 0) {
      B = abs(B);
    }
    pair<ll, ll> key = make_pair(A, B);
    if (m.count(key)) {
      m[key]++;
    } else {
      m[key] = 1;
    }
  }

  vector<ll> p2(200001, 1);
  for (int i = 0; i < 200000; i++) {
    p2[i + 1] = 2LL * p2[i];
    p2[i + 1] %= MOD;
  }

  for (auto &kv : m) {
    pair<ll, ll> key = kv.first;
    ll val = kv.second;
    m[key] = 0;
    // cout << key.first << ' ' << key.second << endl;

    pair<ll, ll> inv_key;
    if (key.second <= 0) {
      inv_key = make_pair(-key.second, key.first);
    } else {
      inv_key = make_pair(key.second, -key.first);
    }
    ll inv_val = 0;
    if (m.count(inv_key)) {
      inv_val = m[inv_key];
      m[inv_key] = 0;
    }

    // cout << inv_key.first << ' ' << inv_key.second << ' ' << val << ' ' <<
    // inv_val << endl;
    ans *= (p2[val] + p2[inv_val] - 1) % MOD;
    ans %= MOD;
  }
  cout << ans - 1 << '\\n';
}