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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;

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

ll gcd(ll a, ll b) {
  if (a == 0 && b == 0)
    return 0;
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }
  if (b == 0)
    return a;

  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

int main(void) {
  int N;
  cin >> N;
  vector<ll> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  map<pair<ll, ll>, int> mp;
  vector<pair<ll, ll>> save_pair;
  for (int i = 0; i < N; i++) {
    bool minus_flag = false;
    ll a = A[i], b = B[i];
    if (a < 0) {
      minus_flag = !minus_flag;
      a = -1 * a;
    }
    if (b < 0) {
      minus_flag = !minus_flag;
      b = -1 * b;
    }
    ll waru = gcd(a, b);
    if (waru == 0) {
      if (mp[make_pair(0, 0)] == 0) {
        save_pair.push_back(make_pair(0, 0));
      }
      mp[make_pair(0, 0)]++;
    } else {
      a /= waru;
      b /= waru;
      if (a == 0 || b == 0) {
        if (b == 0)
          a = -1;
      } else if (minus_flag)
        a *= -1;
      if (mp[make_pair(a, b)] == 0) {
        save_pair.push_back(make_pair(a, b));
      }
      mp[make_pair(a, b)]++;
    }
  }

  ll ans = 1;
  ll zero_ans = 0;
  map<pair<ll, ll>, int> tansaku_flag;
  for (int i = 0; i < int(save_pair.size()); i++) {
    ll a = save_pair[i].first, b = save_pair[i].second;
    if (tansaku_flag[make_pair(a, b)] == 0) {
      if (a == 0 && b == 0) {
        zero_ans = mp[make_pair(0, 0)];
        tansaku_flag[make_pair(a, b)] = 1;
      } else {
        ll a2, b2;
        if (a < 0) {
          a2 = b;
          b2 = -a;
        } else {
          a2 = -b;
          b2 = a;
        }
        ll baai = (modpow(2, mp[make_pair(a, b)], MOD) +
                   modpow(2, mp[make_pair(a2, b2)], MOD)) %
                  MOD;
        baai -= 1;
        if (baai < 0)
          baai += MOD;
        ans = (ans * baai) % MOD;
        tansaku_flag[make_pair(a, b)] = 1;
        tansaku_flag[make_pair(a2, b2)] = 1;
      }
    }
  }
  ans = (ans + zero_ans) % MOD;
  ans = (ans - 1) % MOD;
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;

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

ll gcd(ll a, ll b) {
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }

  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

int main(void) {
  int N;
  cin >> N;
  vector<ll> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  map<pair<ll, ll>, int> mp;
  vector<pair<ll, ll>> save_pair;
  for (int i = 0; i < N; i++) {
    bool minus_flag = false;
    ll a = A[i], b = B[i];
    if (a < 0) {
      minus_flag = !minus_flag;
      a = -1 * a;
    }
    if (b < 0) {
      minus_flag = !minus_flag;
      b = -1 * b;
    }

    ll waru = gcd(a, b);
    if (waru == 0) {
      if (mp[make_pair(0, 0)] == 0) {
        save_pair.push_back(make_pair(0, 0));
      }
      mp[make_pair(0, 0)]++;
    } else {
      a /= waru;
      b /= waru;
      if (minus_flag)
        a *= -1;
      if (mp[make_pair(a, b)] == 0) {
        save_pair.push_back(make_pair(a, b));
      }
      mp[make_pair(a, b)]++;
    }
  }

  ll ans = 1;
  ll zero_ans = 0;
  map<pair<ll, ll>, int> tansaku_flag;
  // for (int i = 0; i < save_pair.size(); i++)
  //     cout << save_pair[i].first << " " << save_pair[i].second << endl;
  for (int i = 0; i < int(save_pair.size()); i++) {
    ll a = save_pair[i].first, b = save_pair[i].second;
    if (tansaku_flag[make_pair(a, b)] == 0) {
      if (a == 0 && b == 0) {
        zero_ans = mp[make_pair(0, 0)];
        tansaku_flag[make_pair(a, b)] = 1;
      } else {
        ll a2, b2;
        if (a < 0) {
          a2 = b;
          b2 = -a;
        } else {
          a2 = -b;
          b2 = a;
        }
        ans = (ans * (((modpow(2, mp[make_pair(a, b)], MOD) +
                        modpow(2, mp[make_pair(a2, b2)], MOD)) %
                       MOD) -
                      1)) %
              MOD;
        tansaku_flag[make_pair(a, b)] = 1;
        tansaku_flag[make_pair(a2, b2)] = 1;
      }
    }
  }
  cout << (ans + zero_ans - 1) % MOD << endl;
}