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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

const int mod = 1e9 + 7;
using modint = ModInt<mod>;

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

int main() {
  int n;
  cin >> n;
  map<pair<ll, ll>, int> m;
  int zero = 0;
  rep(i, n) {
    ll p, q;
    cin >> p >> q;
    if (p == 0 && q == 0) {
      zero++;
      continue;
    } else if (p == 0) {
      m[make_pair(0, 1)]++;
      continue;
    } else if (q == 0) {
      m[make_pair(1, 0)]++;
      continue;
    }
    ll g = gcd(abs(p), abs(q));
    p /= g;
    q /= g;

    m[make_pair(p, q)]++;
  }
  for (auto &e : m) {
    auto p = e.first;
    auto amount = e.second;
    if (amount == 0) {
      continue;
    } else {
      e.second += m[make_pair(-p.first, -p.second)];
      m[make_pair(-p.first, -p.second)] = 0;
    }
  }
  modint ans = 1;
  modint a = 2;
  for (auto &e : m) {
    auto p = e.first;
    auto amount = e.second;
    if (amount == 0) {
      continue;
    }
    auto ng0 = make_pair(-p.second, p.first);
    auto ng1 = make_pair(p.second, -p.first);
    int ngamount = m[ng0] + m[ng1];
    ans *= a.pow(ngamount) + a.pow(amount) - 1;
    m[p] = m[ng0] = m[ng1] = 0;
  }
  cout << ans + zero - 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

const int mod = 1e9 + 7;
using modint = ModInt<mod>;

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

int main() {
  int n;
  cin >> n;
  map<pair<ll, ll>, int> m;
  int zero = 0;
  rep(i, n) {
    ll p, q;
    cin >> p >> q;
    ll g = gcd(abs(p), abs(q));
    p /= g;
    q /= g;
    if (p == 0 && q == 0) {
      zero++;
      continue;
    } else if (p == 0) {
      q = 1;
    } else if (q == 0) {
      p = 1;
    }
    m[make_pair(p, q)]++;
  }
  for (auto &e : m) {
    auto p = e.first;
    auto amount = e.second;
    if (amount == 0) {
      continue;
    } else {
      e.second += m[make_pair(-p.first, -p.second)];
      m[make_pair(-p.first, -p.second)] = 0;
    }
  }
  modint ans = 1;
  modint a = 2;
  for (auto &e : m) {
    auto p = e.first;
    auto amount = e.second;
    if (amount == 0) {
      continue;
    }
    auto ng0 = make_pair(-p.second, p.first);
    auto ng1 = make_pair(p.second, -p.first);
    int ngamount = m[ng0] + m[ng1];
    ans *= a.pow(ngamount) + a.pow(amount) - 1;
    m[p] = m[ng0] = m[ng1] = 0;
  }
  cout << ans + zero - 1 << endl;
}