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

/* #include <bits/stdc++.h> */
#include <deque>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

//
struct pair_hash {
  template <class T1, class T2>
  std::size_t operator()(const std::pair<T1, T2> &p) const {
    auto h1 = std::hash<T1>{}(p.first);
    auto h2 = std::hash<T2>{}(p.second);

    // Mainly for demonstration purposes, i.e. works but is overly simple
    // In the real world, use sth. like boost.hash_combine
    return h1 ^ h2;
  }
};
//

ll gcd(ll x, ll y) {
  if (x > y)
    swap(x, y);
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

ll euclid(ll a, ll b, ll &x, ll &y) {
  if (b) {
    ll d = euclid(b, a % b, y, x);
    return y -= a / b * x, d;
  }
  return x = 1, y = 0, a;
}

const ll mod = 1000000007;
struct Mod {
  ll x;
  Mod(ll xx) : x(xx) {}
  Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
  Mod operator-(Mod b) { return Mod((x - b.x + mod) % mod); }
  Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
  Mod operator/(Mod b) { return *this * invert(b); }
  Mod invert(Mod a) {
    ll x, y, g = euclid(a.x, mod, x, y);
    return Mod((x + mod) % mod);
  }
  Mod operator^(ll e) {
    if (!e)
      return Mod(1);
    Mod r = *this ^ (e / 2);
    r = r * r;
    return e & 1 ? *this * r : r;
  }
};

void solve() {
  int N;
  cin >> N;
  unordered_map<pair<ll, ll>, pair<int, int>, pair_hash> ok;
  ll AB0 = 0;
  for (int i = 0; i < N; i++) {
    ll A, B;
    cin >> A >> B;
    if (A == 0 && B == 0) {
      AB0++;
      continue;
    }
    ll G = gcd(abs(A), abs(B));
    A /= G;
    B /= G;
    int nrots = 0;
    while (A < 0 || B <= 0) {
      A *= -1;
      swap(A, B);
      nrots++;
    }
    pii curr = ok[{A, B}];
    if (nrots % 2) {
      curr.first++;
    } else {
      curr.second++;
    }
    ok[{A, B}] = curr;
  }
  Mod sol(1);
  for (pair<pair<ll, ll>, pair<int, int>> p : ok) {
    int x, y;
    tie(x, y) = p.second;
    Mod a = (Mod(2) ^ x) - 1;
    Mod b = (Mod(2) ^ y) - 1;
    sol = sol * (Mod(1) + a + b);
  }
  sol = sol - Mod(1);
  sol = sol + AB0;
  cout << (sol.x % mod + mod) % mod << "\\n";
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  solve();
}


Error: 0

Fixed submission:

/* #include <bits/stdc++.h> */
#include <deque>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

//
struct pair_hash {
  template <class T1, class T2>
  std::size_t operator()(const std::pair<T1, T2> &p) const {
    auto h1 = std::hash<T1>{}(p.first);
    auto h2 = std::hash<T2>{}(p.second);

    // Mainly for demonstration purposes, i.e. works but is overly simple
    // In the real world, use sth. like boost.hash_combine
    return h1 ^ h2;
  }
};
//

ll gcd(ll x, ll y) {
  if (x > y)
    swap(x, y);
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

ll euclid(ll a, ll b, ll &x, ll &y) {
  if (b) {
    ll d = euclid(b, a % b, y, x);
    return y -= a / b * x, d;
  }
  return x = 1, y = 0, a;
}

const ll mod = 1000000007;
struct Mod {
  ll x;
  Mod(ll xx) : x(xx) {}
  Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
  Mod operator-(Mod b) { return Mod((x - b.x + mod) % mod); }
  Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
  Mod operator/(Mod b) { return *this * invert(b); }
  Mod invert(Mod a) {
    ll x, y, g = euclid(a.x, mod, x, y);
    return Mod((x + mod) % mod);
  }
  Mod operator^(ll e) {
    if (!e)
      return Mod(1);
    Mod r = *this ^ (e / 2);
    r = r * r;
    return e & 1 ? *this * r : r;
  }
};

void solve() {
  int N;
  cin >> N;
  unordered_map<pair<ll, ll>, pair<int, int>, pair_hash> ok;
  for (int i = 0; i < N; i++) {
    ll A, B;
    cin >> A >> B;
    ll G = gcd(abs(A), abs(B));
    A /= G;
    B /= G;
    int nrots = 0;
    while (A < 0 || B < 0) {
      A *= -1;
      swap(A, B);
      nrots++;
    }
    pii curr = ok[{A, B}];
    if (nrots % 2) {
      curr.first++;
    } else {
      curr.second++;
    }
    ok[{A, B}] = curr;
  }
  Mod sol(1);
  for (pair<pair<ll, ll>, pair<int, int>> p : ok) {
    int x, y;
    tie(x, y) = p.second;
    Mod a = (Mod(2) ^ x) - 1;
    Mod b = (Mod(2) ^ y) - 1;
    sol = sol * (Mod(1) + a + b);
  }
  sol = sol - Mod(1);
  cout << sol.x << "\\n";
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  solve();
}