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

#include <bits/stdc++.h>
//<(")
#define ll long long
using namespace std;

typedef pair<ll, ll> ii;
typedef pair<ii, ii> iii;

const ll mod = 1e9 + 7;
const ll siz = 2e5 + 10;
const ll base = 32;
const ll MAXX = 1e18;

ll n;
map<ii, ll> cnt;

ll Pow(ll a, ll b) {
  if (b == 0) {
    return 1;
  }
  ll sub = Pow(a, b / 2);
  if (b % 2 == 0) {
    return sub * sub % mod;
  }
  return (a * (sub * sub % mod)) % mod;
}

bool check(ii val) { return (cnt.find(val) != cnt.end()); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("INP.txt", "r", stdin);
  // freopen("OUT.txt", "w", stdout);
  cin >> n;
  ll djtmemay = 0;
  for (ll i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    ll g = __gcd(abs(a), abs(b));
    if (ii(a, b) == ii(0LL, 0LL)) {
      djtmemay++;
      continue;
    }
    a /= g;
    b /= g;
    if (a == 0) {
      b = 1;
    } else if (b == 0) {
      a = -1;
    } else if (b < 0) {
      a *= -1;
      b *= -1;
    }
    // cerr << a << ' ' << b << '\\n';
    cnt[ii(a, b)]++;
  }
  // for (auto i = cnt.begin(); i != cnt.end(); i++) {
  // cerr << (*i).first.first << ' ' << (*i).first.second << ' ' << (*i).second
  // << '\\n';
  // }
  // cerr << "\\n\\n";
  ll ans = 1;
  for (auto i = cnt.begin(); i != cnt.end(); i++) {
    ll val = (*i).second;
    if (!val) {
      continue;
    }
    bool sub = false;
    ii pos = (*i).first;
    swap(pos.first, pos.second);
    pos.first *= -1;
    if (check(pos) && cnt[pos] > 0) {
      // cerr << "Gei1 : " << ' ' << (*i).first.first << ' ' <<
      // (*i).first.second << '\\n';
      ll x = cnt[pos];
      ans = (ans * ((Pow(2, val) + Pow(2, x) % mod) - 1) % mod) % mod;
      cnt[pos] = 0;
      // cerr << ans << '\\n';
      continue;
    }
    pos = (*i).first;
    swap(pos.first, pos.second);
    pos.second *= -1;
    if (check(pos) && cnt[pos] > 0) {
      // cerr << "Gei 1: " << ' ' << (*i).first.first << ' ' <<
      // (*i).first.second << '\\n';
      ll x = cnt[pos];
      ans = (ans * ((Pow(2, val) + Pow(2, x) % mod) - 1) % mod) % mod;
      cnt[pos] = 0;
      // cerr << ans << '\\n';
      continue;
    }
    // cerr << "Gei 2: " << ' ' << (*i).first.first << ' ' << (*i).first.second
    // << '\\n';
    ans = (ans * Pow(2, val)) % mod;
    // cerr << ans << '\\n';
  }
  ans = ((ans - 1) + mod * mod) % mod;
  cout << (ans + djtmemay) % mod << '\\n';
  return 0;
}

Error: WA

Fixed submission:

// In the name of God

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;
const int mod = 1e9 + 7;

using ll = long long;

pair<ll, ll> a[N];
ll ans;
int n;

ll fast_pw(ll x, ll y) {
  x %= mod;
  ll ret = 1;

  for (; y; y >>= 1) {
    if (y & 1)
      ret = (ret * x) % mod;
    x = (x * x) % mod;
  }
  return ret;
}

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

  cin >> n;

  int t1 = 0, t2 = 0;

  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;

    assert(a[i].first || a[i].second);

    if (a[i].first == 0 && a[i].second)
      t1++;
    if (a[i].second == 0 && a[i].first)
      t2++;
  }

  map<pair<ll, ll>, int> neg, pos;

  for (int i = 0; i < n; i++) {

    if (a[i].first == 0 || a[i].second == 0)
      continue;

    bool f = true;
    pair<ll, ll> now = a[i];

    if ((now.first < 0) != (now.second < 0))
      f = false;

    now.first = abs(now.first);
    now.second = abs(now.second);

    ll g = __gcd(now.first, now.second);

    now.first /= g;
    now.second /= g;

    if (f) {
      pos[now]++;
    } else {
      neg[now]++;
    }
  }

  vector<pair<int, int>> vp;

  for (auto &x : pos) {
    vp.emplace_back(x.second, neg[make_pair(x.first.second, x.first.first)]);
  }

  for (auto &x : neg) {
    if (pos.find(make_pair(x.first.second, x.first.first)) != pos.end())
      continue;

    vp.emplace_back(x.second, 0);
  }

  if (t1 || t2)
    vp.emplace_back(t1, t2);

  ans = 1;

  for (auto &p : vp) {
    ll x = ans *
           ((fast_pw(2, p.first) - 1 + mod) % mod +
            (fast_pw(2, p.second) - 1 + mod) % mod) %
           mod;
    x %= mod;
    ans = (ans + x) % mod;
  }

  cout << (ans - 1 + mod) % mod;

  return 0;
}