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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

class Mod {
public:
  ll mod;
  ll kaijou[200005];
  ll inv[200005];
  // mod must be a prime number
  Mod(ll m) {
    mod = m;
    kaijou[0] = 1;
    for (ll i = 1; i <= 200003; i++) {
      kaijou[i] = kaijou[i - 1] * i % mod;
      inv[i] = modpow(kaijou[i], mod - 2);
    }
  }
  // a^p
  ll modpow(ll a, ll p) {
    if (p == 0)
      return 1;
    if (p % 2 == 0) {
      ll half = modpow(a, p / 2);
      return half * half % mod;
    } else
      return a * modpow(a, p - 1) % mod;
  }

  // nCr
  ll comb(ll n, ll r) {
    if (r > n - r)
      return comb(n, n - r);
    ll mul = 1;
    ll div = 1;
    for (int i = 0; i < r; i++) {
      mul *= n - i;
      mul %= mod;
      div *= i + 1;
      div %= mod;
    }
    return mul * modpow(div, mod - 2) % mod;
  }

  ll fcomb(ll n, ll r) {
    if (r > n - r)
      return fcomb(n, n - r);
    if (r == 0)
      return 1;
    return (kaijou[n] * inv[n - r] % mod) * inv[r] % mod;
  }
};

long long lgcd(long long x, long long y) {
  if (x == 0)
    return y;
  return lgcd(y % x, x);
}

ll N;
ll A[200005], B[200005];

map<P, bool> used;

map<P, ll> mp;

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

  Mod m(1e9 + 7);

  cin >> N;
  ll cnt2 = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    if (A[i] == 0 && B[i] == 0) {
      cnt2++;
      continue;
    } else if (A[i] == 0) {
      mp[P(0, 1)]++;
      continue;
    } else if (B[i] == 0) {
      mp[P(1, 0)]++;
      continue;
    }
    ll g = lgcd(abs(A[i]), abs(B[i]));
    A[i] /= g;
    B[i] /= g;
    if (A[i] < 0) {
      A[i] *= -1;
      B[i] *= -1;
    }
    mp[P(A[i], B[i])]++;
    // cout << A[i] << " " << B[i] << " sadfasd" << endl;
  }

  ll cnt = 0;
  ll ans = 1;

  bool f = false;

  for (const auto &[key, value] : mp) {
    // std::cout << key.first << " => " << value << "\\n"
    // cout << "dfsdfs" << endl;
    if (used[key])
      continue;
    ll va = value;
    ll a = key.second, b = -key.first;
    if (a < 0) {
      a *= -1;
      b *= -1;
    }
    used[P(a, b)] = true;
    used[key] = true;
    ll vb = mp[P(a, b)];
    // cout << va << " " << vb << " " <<
    // (m.modpow(2,va)+m.modpow(2,vb)+m.mod-1)%m.mod << " " << key.first << " "
    // << key.second << endl;
    ans *= (m.modpow(2, va) + m.modpow(2, vb) + m.mod - 1) % m.mod;
    cnt++;
    if (vb == 0)
      f = true;
    ans %= m.mod;
  }

  ans += cnt2;
  ans %= m.mod;
  cout << (ans + m.mod - 1) % m.mod << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

class Mod {
public:
  ll mod;
  ll kaijou[200005];
  ll inv[200005];
  // mod must be a prime number
  Mod(ll m) {
    mod = m;
    kaijou[0] = 1;
    for (ll i = 1; i <= 200003; i++) {
      kaijou[i] = kaijou[i - 1] * i % mod;
      inv[i] = modpow(kaijou[i], mod - 2);
    }
  }
  // a^p
  ll modpow(ll a, ll p) {
    if (p == 0)
      return 1;
    if (p % 2 == 0) {
      ll half = modpow(a, p / 2);
      return half * half % mod;
    } else
      return a * modpow(a, p - 1) % mod;
  }

  // nCr
  ll comb(ll n, ll r) {
    if (r > n - r)
      return comb(n, n - r);
    ll mul = 1;
    ll div = 1;
    for (int i = 0; i < r; i++) {
      mul *= n - i;
      mul %= mod;
      div *= i + 1;
      div %= mod;
    }
    return mul * modpow(div, mod - 2) % mod;
  }

  ll fcomb(ll n, ll r) {
    if (r > n - r)
      return fcomb(n, n - r);
    if (r == 0)
      return 1;
    return (kaijou[n] * inv[n - r] % mod) * inv[r] % mod;
  }
};

ll N;
ll A[200005], B[200005];

map<P, bool> used;

map<P, ll> mp;

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

  Mod m(1e9 + 7);

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    ll g = __gcd(abs(A[i]), abs(B[i]));
    A[i] /= g;
    B[i] /= g;
    if (A[i] < 0) {
      A[i] *= -1;
      B[i] *= -1;
    }
    mp[P(A[i], B[i])]++;
    // cout << A[i] << " " << B[i] << " sadfasd" << endl;
  }

  ll ans = 1;

  for (const auto &[key, value] : mp) {
    // std::cout << key.first << " => " << value << "\\n"
    // cout << "dfsdfs" << endl;
    if (used[key])
      continue;
    ll va = value;
    ll a = key.second, b = -key.first;
    if (a < 0) {
      a *= -1;
      b *= -1;
    }
    used[P(a, b)] = true;
    used[key] = true;
    ll vb = mp[P(a, b)];
    // cout << va << " " << vb << " " <<
    // (m.modpow(2,va)+m.modpow(2,vb)+m.mod-1)%m.mod << " " << key.first << " "
    // << key.second << endl;
    ans *= (m.modpow(2, va) + m.modpow(2, vb) + m.mod - 1) % m.mod;
    ans %= m.mod;
  }

  cout << (ans + m.mod - 1) % m.mod << endl;

  return 0;
}