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

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

ll rep_square(ll a, ll b) {
  // return a^b
  ll ans = 1;
  ll base = a;
  while (b != 0) {
    if (b & 1) {
      ans *= base;
      ans %= mod;
    }
    base = (base * base) % mod;
    b >>= 1;
  }
  return ans;
}

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

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
source:
  ll n;
  cin >> n;
  ll a[n], b[n];
  ll zero_zero = 0;
  map<pair<ll, ll>, ll> map0, map1;

  rep(i, n) {
    cin >> a[i] >> b[i];

    if (a[i] == 0 && b[i] == 0) {
      zero_zero++;
      continue;
    } else if (a[i] == 0) {
      b[i] = 1;
      map1[make_pair(1, 0)]++;
      continue;
    } else if (b[i] == 0) {
      a[i] = 1;
      map0[make_pair(1, 0)]++;
      continue;
    }

    if (a[i] > 0 && b[i] > 0) {
      ll g = gcd(a[i], b[i]);
      a[i] /= g;
      b[i] /= g;
      map0[make_pair(a[i], b[i])]++;
    } else if (a[i] < 0 && b[i] < 0) {
      a[i] = -a[i];
      b[i] = -b[i];
      ll g = gcd(a[i], b[i]);
      a[i] /= g;
      b[i] /= g;
      map0[make_pair(a[i], b[i])]++;
    } else if (a[i] > 0 && b[i] < 0) {
      b[i] = -b[i];
      swap(a[i], b[i]);
      ll g = gcd(a[i], b[i]);
      a[i] /= g;
      b[i] /= g;
      map1[make_pair(a[i], b[i])]++;
    } else if (a[i] < 0 && b[i] > 0) {
      a[i] = -a[i];
      swap(a[i], b[i]);
      ll g = gcd(a[i], b[i]);
      a[i] /= g;
      b[i] /= g;
      map1[make_pair(a[i], b[i])]++;
    }
  }

  ll num = n - zero_zero;

  map<pair<ll, ll>, pair<ll, ll>> map_all;
  for (auto iter = map0.begin(); iter != map0.end(); iter++) {
    map_all[iter->first].first += iter->second;
  }
  for (auto iter = map1.begin(); iter != map1.end(); iter++) {
    map_all[iter->first].second += iter->second;
  }

  ll ans = 1;
  for (auto iter = map_all.begin(); iter != map_all.end(); iter++) {
    auto x = iter->first;
    auto y = iter->second;

    ll num0 = y.first, num1 = y.second;

    ll temp = (rep_square(2, num0) + rep_square(2, num1) - 1) % mod;

    ans = (ans * temp) % mod;
  }

  ans--;
  ans += zero_zero;
  ans = (ans + mod) % mod;

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll mod = 1000000007;

ll rep_square(ll a, ll b) {
  // return a^b
  ll ans = 1;
  ll base = a;
  while (b != 0) {
    if (b & 1) {
      ans *= base;
      ans %= mod;
    }
    base = (base * base) % mod;
    b >>= 1;
  }
  return ans;
}

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

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
source:
  ll n;
  cin >> n;
  ll a[n], b[n];
  ll zero_zero = 0;
  map<pair<ll, ll>, ll> map0, map1;

  rep(i, n) {
    cin >> a[i] >> b[i];

    if (a[i] == 0 && b[i] == 0) {
      zero_zero++;
      continue;
    } else if (a[i] == 0) {
      b[i] = 1;
      map1[make_pair(1, 0)]++;
      continue;
    } else if (b[i] == 0) {
      a[i] = 1;
      map0[make_pair(1, 0)]++;
      continue;
    }

    if (a[i] > 0 && b[i] > 0) {
      ll g = gcd(a[i], b[i]);
      a[i] /= g;
      b[i] /= g;
      map0[make_pair(a[i], b[i])]++;
    } else if (a[i] < 0 && b[i] < 0) {
      a[i] = -a[i];
      b[i] = -b[i];
      ll g = gcd(a[i], b[i]);
      a[i] /= g;
      b[i] /= g;
      map0[make_pair(a[i], b[i])]++;
    } else if (a[i] > 0 && b[i] < 0) {
      b[i] = -b[i];
      swap(a[i], b[i]);
      ll g = gcd(a[i], b[i]);
      a[i] /= g;
      b[i] /= g;
      map1[make_pair(a[i], b[i])]++;
    } else if (a[i] < 0 && b[i] > 0) {
      a[i] = -a[i];
      swap(a[i], b[i]);
      ll g = gcd(a[i], b[i]);
      a[i] /= g;
      b[i] /= g;
      map1[make_pair(a[i], b[i])]++;
    }
  }

  ll num = n - zero_zero;

  map<pair<ll, ll>, pair<ll, ll>> map_all;
  for (auto iter = map0.begin(); iter != map0.end(); iter++) {
    map_all[iter->first].first += iter->second;
  }
  for (auto iter = map1.begin(); iter != map1.end(); iter++) {
    map_all[iter->first].second += iter->second;
  }

  ll ans = 1;
  for (auto iter = map_all.begin(); iter != map_all.end(); iter++) {
    auto x = iter->first;
    auto y = iter->second;

    ll num0 = y.first, num1 = y.second;

    ll temp = (rep_square(2, num0) + rep_square(2, num1) - 1) % mod;

    ans = (ans * temp) % mod;
  }

  if (ans == 0) {
    throw runtime_error("e");
    cout << zero_zero << endl;
    return 0;
  }
  ans--;
  ans += zero_zero;

  cout << ans << endl;
}