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

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void input_init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

template <typename T> T GCD(T a, T b) {
  if (a % b == 0)
    return b;
  return GCD(b, a % b);
}

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; }
};

using modint = ModInt<MOD>;

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

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  modint zero = 0;
  ll lz = 0;
  ll rz = 0;
  map<P, ll> mp;
  set<P> st;
  rep(i, n) {
    cin >> a[i] >> b[i];

    if (a[i] == 0 && b[i] == 0) {
      zero += 1;
    } else if (a[i] == 0 && b[i] != 0) {
      lz += 1;
    } else if (a[i] != 0 && b[i] == 0) {
      rz += 1;
    } else {
      ll A = a[i];
      ll B = b[i];
      bool minus = false;
      if ((A < 0 && B >= 0) || (A >= 0 && B < 0))
        minus = true;
      A = abs(A);
      B = abs(B);
      ll g = GCD(A, B);
      A /= g;
      B /= g;
      if (minus)
        A *= -1;
      mp[make_pair(A, B)]++;
    }
  }

  modint ans = 1;
  for (auto &&e : mp) {
    if (st.count(e.first))
      continue;
    ll A = e.first.first;
    ll B = e.first.second;
    ll cnt = e.second;

    ll _A = B;
    ll _B = A;
    if (A < 0) {
      _B = abs(_B);
      _A *= -1;
    }
    _A *= -1;

    // cout << "A = " << A << ", B = " << B << '\\n';
    // cout << "_A = " << _A << ", _B = " << _B << '\\n';
    P _p = make_pair(_A, _B);
    ll _cnt = 0;
    if (mp.count(_p))
      _cnt = mp[_p];
    modint tmp = (mpow(2, cnt)) + (mpow(2, _cnt) - 1);
    ans *= tmp;
    // cout << "tmp = " << tmp << '\\n';
    st.insert(make_pair(_A, _B));
  }
  ans *= mpow(2, lz) + mpow(2, rz) - 1;
  ans += zero;

  ans -= 1;
  cout << ans << '\\n';

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void input_init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

template <typename T> T gcd(T a, T b) {
  if (a % b == 0)
    return b;
  gcd(b, a % b);
}

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; }
};

using modint = ModInt<MOD>;

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

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  ll zero = 0;
  map<P, ll> mp;
  set<P> st;
  rep(i, n) {
    cin >> a[i] >> b[i];

    if (a[i] == 0 || b[i] == 0) {
      zero++;
    } else {
      ll A = a[i];
      ll B = b[i];
      bool minus = false;
      if ((A < 0 && B >= 0) || (A >= 0 && B < 0))
        minus = true;
      A = abs(A);
      B = abs(B);
      ll g = gcd(A, B);
      A /= g;
      B /= g;
      if (minus)
        A *= -1;
      mp[make_pair(A, B)]++;
    }
  }

  modint ans = 1;
  for (auto &&e : mp) {
    if (st.count(e.first))
      continue;
    ll A = e.first.first;
    ll B = e.first.second;
    ll cnt = e.second;

    ll _A = B;
    ll _B = A;
    if (A < 0) {
      _B = abs(_B);
      _A *= -1;
    }
    _A *= -1;

    // cout << "A = " << A << ", B = " << B << '\\n';
    // cout << "_A = " << _A << ", _B = " << _B << '\\n';
    P _p = make_pair(_A, _B);
    ll _cnt = 0;
    if (mp.count(_p))
      _cnt = mp[_p];
    modint tmp = (mpow(2, cnt)) + (mpow(2, _cnt) - 1);
    ans *= tmp;
    // cout << "tmp = " << tmp << '\\n';
    st.insert(make_pair(_A, _B));
  }
  ans += zero;

  ans -= 1;
  cout << ans << '\\n';

  return 0;
}