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

#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
ll n, cnt, c, d, tmp, m, h, w, x, y, sum, k, q;
map<pair<P, ll>, ll> le, ri, mem;
ll rui[ohara], dat[ohara];
ld inf = (ld)1000000000000000100.0;
V a, b;
ll le_ze, ri_ze, zero;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  using mint = modint<MOD>;

  cin >> n;
  rui[0] = 1;
  rrep(i, 1, n + 10) {
    rui[i] = rui[i - 1] * 2;
    rui[i] %= MOD;
  }
  rep(i, n) {
    cin >> c >> d;
    // if (c == 0 && d == 0) return -1;
    if (c == 0 && d == 0)
      zero++;
    else if (c == 0)
      le_ze++;
    else if (d == 0)
      ri_ze++;
    else {
      ll see = 1;
      if (c < 0 && d > 0)
        see = 0;
      else if (c > 0 && d < 0)
        see = 0;
      if (c < 0)
        c *= -1;
      if (d < 0)
        d *= -1;
      tmp = __gcd(c, d);
      c /= tmp;
      d /= tmp;
      le[{{c, d}, see}]++;
      ri[{{d, c}, see}]++;
      // debug(c);
      dat[cnt++] = see;
      a.p_b(c);
      b.p_b(d);
    }
  }
  n = Size(a);
  mint ans = mint(1);
  rep(i, n) {
    ll see, now = dat[i];
    if (now == 0)
      see = 1;
    else
      see = 0;
    // debug("AAA");
    // debug(b[i]);
    // Cout((le[{{a[i], b[i]}, now}]));
    if (le[{{a[i], b[i]}, now}] == 0 || mem[{{a[i], b[i]}, now}])
      continue;
    mint kake;
    // debug("AAA");
    if (ri[{{a[i], b[i]}, see}] != 0) {
      kake = mint(rui[le[{{a[i], b[i]}, now}]]);
      kake -= mint(1);
      kake += mint(rui[ri[{{a[i], b[i]}, see}]]);
      le[{{a[i], b[i]}, now}] = 0;
      mem[{{b[i], a[i]}, see}] = 1;
      ans *= kake;
    } else {
      kake = mint(rui[le[{{a[i], b[i]}, now}]]);
      le[{{a[i], b[i]}, now}] = 0;
      ans *= kake;
    }
    // Cout(ans.value());
    // debug(i);
  }
  // Cout(n);
  mint kake;
  if (ri_ze) {
    kake = mint(rui[le_ze]);
    kake -= mint(1);
    kake += mint(rui[ri_ze]);
    ans *= kake;
  } else {
    kake = mint(rui[le_ze]);
    ans *= kake;
  }
  ans -= mint(1);
  ans += mint(zero);
  Cout(ans.value());
  return 0;
}

Error: WA

Fixed submission:

#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
ll n, cnt, c, d, tmp, m, h, w, x, y, sum, k, q;
map<pair<P, ll>, ll> le, ri, mem;
ll rui[ohara], dat[ohara];
ld inf = (ld)1000000000000000100.0;
V a, b;
ll le_ze, ri_ze;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  using mint = modint<MOD>;

  cin >> n;
  rui[0] = 1;
  rrep(i, 1, n + 10) {
    rui[i] = rui[i - 1] * 2;
    rui[i] %= MOD;
  }
  rep(i, n) {
    cin >> c >> d;
    if (c == 0 && d == 0)
      return -1;
    if (c == 0 && d == 0)
      cnt++;
    else if (c == 0)
      le_ze++;
    else if (d == 0)
      ri_ze++;
    else {
      ll see = 1;
      if (c < 0 && d > 0)
        see = 0;
      else if (c > 0 && d < 0)
        see = 0;
      if (c < 0)
        c *= -1;
      if (d < 0)
        d *= -1;
      tmp = __gcd(c, d);
      c /= tmp;
      d /= tmp;
      le[{{c, d}, see}]++;
      ri[{{d, c}, see}]++;
      // debug(c);
      dat[cnt++] = see;
      a.p_b(c);
      b.p_b(d);
    }
  }
  n = Size(a);
  mint ans = mint(1);
  rep(i, n) {
    ll see, now = dat[i];
    if (now == 0)
      see = 1;
    else
      see = 0;
    // debug("AAA");
    // debug(b[i]);
    // Cout((le[{{a[i], b[i]}, now}]));
    if (le[{{a[i], b[i]}, now}] == 0 || mem[{{a[i], b[i]}, now}])
      continue;
    mint kake;
    // debug("AAA");
    if (ri[{{a[i], b[i]}, see}] != 0) {
      kake = mint(rui[le[{{a[i], b[i]}, now}]]);
      kake -= mint(1);
      kake += mint(rui[ri[{{a[i], b[i]}, see}]]);
      le[{{a[i], b[i]}, now}] = 0;
      mem[{{b[i], a[i]}, see}] = 1;
      ans *= kake;
    } else {
      kake = mint(rui[le[{{a[i], b[i]}, now}]]);
      le[{{a[i], b[i]}, now}] = 0;
      ans *= kake;
    }
    // Cout(ans.value());
    // debug(i);
  }
  // Cout(n);
  mint kake;
  if (ri_ze) {
    kake = mint(rui[le_ze]);
    kake -= mint(1);
    kake += mint(rui[ri_ze]);
    ans *= kake;
  } else {
    kake = mint(rui[le_ze]);
    ans *= kake;
  }
  ans -= mint(1);
  ans += mint(cnt);
  Cout(ans.value());
  return 0;
}