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

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

#include <bits/stdc++.h>
using namespace std;

template <typename T> void cmpmax(T &reference, T value) {
  reference = max(reference, value);
}

template <typename T> void cmpmin(T &reference, T value) {
  reference = min(reference, value);
}

const ll mod = 1000000007;

static const ll INF = 1e15;

ll mpow(ll x, ll n) {
  ll ans = 1;
  x %= mod;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

ll gcd(ll a, ll b) {
  while (b)
    a %= b, swap(a, b);
  return abs(a);
}

int main() {
  ll n;
  cin >> n;
  vector<ll> as(n), bs(n);
  map<P, ll> norm;
  ll wolf = 0;
  ll azero = 0;
  ll bzero = 0;
  rep(i, n) {
    cin >> as[i] >> bs[i];
    if (as[i] == 0 && bs[i] == 0) {
      wolf++;
      continue;
    }
    if (as[i] == 0) {
      azero++;
      continue;
    }
    if (bs[i] == 0) {
      bzero++;
      continue;
    }
    auto val = gcd(as[i], bs[i]);
    as[i] /= val;
    bs[i] /= val;
    if (as[i] < 0) {
      as[i] *= -1;
      bs[i] *= -1;
    }
    norm[make_pair(as[i], bs[i])]++;
  }
  set<P> visit;
  ll sum = 1;
  if (azero || bzero) {
    ll c = 0;
    if (azero) {
      c += mpow(2, azero) - 1;
    }
    if (bzero) {
      c += mpow(2, bzero) - 1;
    }
    c++;
    c %= mod;
    sum *= c;
    sum %= mod;
  }
  for (auto p : norm) {
    auto key = p.first;
    if (visit.count(key))
      continue;
    visit.insert(key);
    auto f1 = key.first;
    auto f2 = key.second;
    if (f2 < 0) {
      f1 *= -1;
      f2 *= -1;
    }
    auto invKey = make_pair(f2, -f1);
    visit.insert(invKey);
    int n1 = p.second;
    int n2 = norm[invKey];

    auto c = mpow(2, n1) - 1;
    if (n2) {
      c += mpow(2, n2) - 1;
    }
    c++;
    c %= mod;
    sum *= c;
    sum %= mod;
  }
  sum += mod - 1;
  sum %= mod;
  sum += wolf;
  sum %= mod;
  cout << sum << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

#include <bits/stdc++.h>
using namespace std;

template <typename T> void cmpmax(T &reference, T value) {
  reference = max(reference, value);
}

template <typename T> void cmpmin(T &reference, T value) {
  reference = min(reference, value);
}

const ll mod = 1000000007;

static const ll INF = 1e15;

ll mpow(ll x, ll n) {
  ll ans = 1;
  x %= mod;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

ll gcd(ll a, ll b) {
  while (b)
    a %= b, swap(a, b);
  return abs(a);
}

int main() {
  ll n;
  cin >> n;
  vector<ll> as(n), bs(n);
  map<P, int> norm;
  rep(i, n) {
    cin >> as[i] >> bs[i];
    auto val = gcd(as[i], bs[i]);
    as[i] /= val;
    bs[i] /= val;
    if (as[i] < 0) {
      as[i] *= -1;
      bs[i] *= -1;
    }
    norm[make_pair(as[i], bs[i])]++;
  }
  set<P> visit;
  ll sum = 1;
  for (auto p : norm) {
    auto key = p.first;
    if (visit.count(key))
      continue;
    visit.insert(key);
    auto f1 = key.first;
    auto f2 = key.second;
    if (f2 < 0) {
      f1 *= -1;
      f2 *= -1;
    }
    auto invKey = make_pair(f2, -f1);
    visit.insert(invKey);
    int n1 = p.second;
    int n2 = norm[invKey];

    auto c = mpow(2, n1) - 1;
    if (n2) {
      c += mpow(2, n2) - 1;
    }
    c++;
    c %= mod;
    sum *= c;
    sum %= mod;
  }
  sum += mod - 1;
  sum %= mod;
  cout << sum << endl;
  return 0;
}