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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define ll long long
#define INF 2147483647
using namespace std;
using Graph = vector<vector<int>>;

ll gcd(ll a, ll b) {
  a = abs(a);
  b = abs(b);
  if (a == 0 || b == 0) {
    return abs(a + 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;
}

ll RepSq(ll a, ll n, ll mod) {
  if (n == 0) {
    return 1;
  } else if (n % 2 != 0) {
    return (a * RepSq(a, n - 1, mod)) % mod;
  } else {
    ll x = RepSq(a, n / 2, mod);
    return (x * x) % mod;
  }
}

int main() {
  ll n;
  cin >> n;
  ll mod = 1000000007;
  ll a[n], b[n], g[n];
  map<pair<ll, ll>, ll> mp;
  ll iwashinainotoissyo = 0;
  rep(i, n) {
    cin >> a[i] >> b[i];
    if (a[i] != 0 || b[i] != 0) {
      g[i] = gcd(a[i], b[i]);
      a[i] /= g[i];
      b[i] /= g[i];
      if (a[i] < 0 || (a[i] == 0 && b[i] > 0)) {
        a[i] *= -1;
        b[i] *= -1;
      }
      mp[make_pair(a[i], b[i])]++;
    } else {
      iwashinainotoissyo++;
    }
  }
  ll ans = 1;
  ll rest = n - iwashinainotoissyo;
  for (auto p : mp) {
    ll x, y, z, w;
    x = p.first.first;
    y = p.first.second;
    z = p.second;
    if (y < 0) {
      w = mp[make_pair(-y, x)];
      if (w != 0) {
        ans = (ans * (RepSq(2, z, mod) + RepSq(2, w, mod) - 1)) % mod;
        rest = rest - z - w;
      }
    }
  }
  ans = (RepSq(2, rest, mod) * ans) % mod;
  cout << (ans + mod - 1 + iwashinainotoissyo) % mod;
}


Error: WA

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define ll long long
#define INF 2147483647
using namespace std;
using Graph = vector<vector<int>>;

ll gcd(ll a, ll b) {
  a = abs(a);
  b = abs(b);
  if (a == 0 || b == 0) {
    return abs(a + 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;
}

ll RepSq(ll a, ll n, ll mod) {
  if (n == 0) {
    return 1;
  } else if (n % 2 != 0) {
    return (a * RepSq(a, n - 1, mod)) % mod;
  } else {
    ll x = RepSq(a, n / 2, mod);
    return (x * x) % mod;
  }
}

int main() {
  ll n;
  cin >> n;
  ll mod = 1000000007;
  ll a[n], b[n], g[n];
  map<pair<ll, ll>, ll> mp;
  rep(i, n) {
    cin >> a[i] >> b[i];
    g[i] = gcd(a[i], b[i]);
    a[i] /= g[i];
    b[i] /= g[i];
    if (a[i] < 0 || (a[i] == 0 && b[i] > 0)) {
      a[i] *= -1;
      b[i] *= -1;
    }
    mp[make_pair(a[i], b[i])]++;
  }
  ll ans = 1;
  ll rest = n;
  for (auto p : mp) {
    ll x, y, z, w;
    x = p.first.first;
    y = p.first.second;
    z = p.second;
    if (y < 0) {
      w = mp[make_pair(-y, x)];
      if (w != 0) {
        ans = (ans * (RepSq(2, z, mod) + RepSq(2, w, mod) - 1)) % mod;
        rest = rest - z - w;
      }
    }
  }
  ans = (RepSq(2, rest, mod) * ans) % mod;
  cout << (ans + mod - 1) % mod;
}