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

// abc168_e.cpp
// Sun May 17 20:36:54 2020

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007

using namespace std;
using ll = long long;
typedef pair<ll, ll> P;

// 最大公倍数、最大公約数（ユークリッド互除法）
ll gcd(ll a, ll b) { // 最大公約数
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}

ll mod_pow(ll x, ll n) {
  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;

  vector<P> num;
  map<P, ll> mp;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    ll kouyaku = gcd(a, b);
    if (kouyaku != 0) {
      a = a / abs(kouyaku);
      b = b / abs(kouyaku);
    }
    auto itr = mp.find(P(a, b));
    if (itr != mp.end()) {
      mp[P(a, b)]++;
    } else {
      mp[P(a, b)] = 1;
    }
  }

  ll ans = 1;
  ll am = 0;
  ll abzero = 0;
  ll agazero = 0;
  ll bgazero = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    ll aite = 0;
    P tmp = itr->first;
    if (mp[tmp] == 0) {
      continue;
    }
    ll count = mp[tmp];
    ll aa = tmp.first;
    ll bb = tmp.second;
    if (aa == 0 && bb == 0) {
      abzero = count;
      continue;
    } else if (aa == 0) {
      agazero += count;
      continue;
    } else if (bb == 0) {
      bgazero += count;
      continue;
    }
    int flag = 1;
    //		cout << aa << " " << bb  << " " << count << endl;
    auto itrtmp = mp.find(P(aa * (-1), bb * (-1)));
    if (itrtmp != mp.end()) {
      count += mp[P(aa * (-1), bb * (-1))];
      mp[P(aa * (-1), bb * (-1))] = 0;
    }

    itrtmp = mp.find(P(bb, aa * (-1)));
    if (itrtmp != mp.end()) {
      aite += mp[P(bb, aa * (-1))];
      mp[P(bb, aa * (-1))] = 0;
      flag = 0;
    }

    itrtmp = mp.find(P(bb * (-1), aa));
    if (itr != mp.end()) {
      aite += mp[P(bb * (-1), aa)];
      mp[P(bb * (-1), aa)] = 0;
      flag = 0;
    }

    if (flag) {
      am += count;
    } else {
      ll tmptmp = (mod_pow(2, count) + mod_pow(2, aite) + MOD - 1) % MOD;
      ans = ans * tmptmp % MOD;
    }
  }

  if (agazero && bgazero) {
    ll tmptmp = (mod_pow(2, agazero) + mod_pow(2, bgazero) + MOD - 1) % MOD;
    ans = ans * tmptmp % MOD;
  } else {
    am += agazero + bgazero;
  }

  ans = ans * mod_pow(2, am) % MOD;

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

  cout << ans << endl;
  //	printf("%.4f\\n",ans);
}

Error: 0

Fixed submission:

// abc168_e.cpp
// Sun May 17 20:36:54 2020

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007

using namespace std;
using ll = long long;
typedef pair<ll, ll> P;

// 最大公倍数、最大公約数（ユークリッド互除法）
ll gcd(ll a, ll b) { // 最大公約数
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}

ll mod_pow(ll x, ll n) {
  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;

  vector<P> num;
  map<P, ll> mp;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    ll kouyaku = gcd(a, b);
    a = a / abs(kouyaku);
    b = b / abs(kouyaku);
    auto itr = mp.find(P(a, b));
    if (itr != mp.end()) {
      mp[P(a, b)]++;
    } else {
      mp[P(a, b)] = 1;
    }
  }

  ll ans = 1;
  ll am = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    ll aite = 0;
    P tmp = itr->first;
    if (mp[tmp] == 0) {
      continue;
    }
    ll count = mp[tmp];
    ll aa = tmp.first;
    ll bb = tmp.second;
    int flag = 1;
    //		cout << aa << " " << bb  << " " << count << endl;
    auto itrtmp = mp.find(P(aa * (-1), bb * (-1)));
    if (itrtmp != mp.end()) {
      count += mp[P(aa * (-1), bb * (-1))];
      mp[P(aa * (-1), bb * (-1))] = 0;
    }

    itrtmp = mp.find(P(bb, aa * (-1)));
    if (itrtmp != mp.end()) {
      aite += mp[P(bb, aa * (-1))];
      mp[P(bb, aa * (-1))] = 0;
      flag = 0;
    }

    itrtmp = mp.find(P(bb * (-1), aa));
    if (itr != mp.end()) {
      aite += mp[P(bb * (-1), aa)];
      mp[P(bb * (-1), aa)] = 0;
      flag = 0;
    }

    if (flag) {
      am += count;
    } else {
      ll tmptmp = (mod_pow(2, count) + mod_pow(2, aite) + MOD - 1) % MOD;
      ans = ans * tmptmp % MOD;
    }
    //		cout <<aa << " " << bb << " " << count << " " << aite << endl;
  }

  //	cout << mp[P(1,2)] << endl;

  ans = ans * mod_pow(2, am) % MOD;

  cout << ans - 1 << endl;
  //	printf("%.4f\\n",ans);
}