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

#include <bits/stdc++.h>
using namespace std;

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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;

ll pow(ll a, ll n, ll m) {
  ll ret = 1LL;
  for (; n > 0LL; n >>= 1LL, a = a * a % m) {
    if (n % 2LL == 1LL) {
      ret = ret * a % m;
    }
  }
  return ret;
}

ll gcd(ll x, ll y) {
  if (x % y == 0) {
    return y;
  } else {
    return gcd(y, x % y);
  }
}

int main() {
  ll N;
  cin >> N;
  map<pair<ll, ll>, ll> m;
  ll cnt = 0LL;
  ll cnto = 0LL;
  ll cntb = 0LL;
  for (ll i = 0LL; i < N; i++) {
    ll A, B;
    cin >> A >> B;
    if (A == 0LL && B != 0LL) {
      cnt++;
      continue;
    }
    if (A == 0LL && B == 0LL) {
      cnto++;
      continue;
    }
    if (A != 0LL && B == 0LL) {
      cntb++;
      continue;
    } else {
      ll t = (abs(A) / A) * (abs(B) / B);
      pair<ll, ll> p = make_pair(t * abs(B) / gcd(abs(A), abs(B)),
                                 abs(A) / gcd(abs(A), abs(B)));
      m[p]++;
    }
  }
  for (auto x : m) {
    ll a = x.first.first;
    ll b = x.first.second;
    pair<ll, ll> p = make_pair(-b * (abs(a) / a), abs(a));
    m[p];
  }
  ll ans = 1LL;
  for (auto x : m) {
    ll A = x.first.first;
    ll B = x.first.second;
    pair<ll, ll> p = make_pair(-B * (abs(A) / A), abs(A));
    ll a = x.second;
    ll &b = m[p];
    ans *= (pow(2LL, a, mod) + pow(2LL, b, mod) - 1LL) % mod;
    ans %= mod;
    m[x.first] = 0LL;
    b = 0LL;
  }
  ans *= (pow(2LL, cnt, mod) + pow(2LL, cntb, mod) - 1LL) % mod;
  ans %= mod;
  ans += cnto % mod;
  ans %= mod;
  ans--;
  if (ans < 0LL)
    ans += mod;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;

ll pow(ll a, ll n, ll m) {
  ll ret = 1LL;
  for (; n > 0LL; n >>= 1LL, a = a * a % m) {
    if (n % 2LL == 1LL) {
      ret = ret * a % m;
    }
  }
  return ret;
}

ll gcd(ll x, ll y) {
  if (x % y == 0) {
    return y;
  } else {
    return gcd(y, x % y);
  }
}

int main() {
  ll N;
  cin >> N;
  map<pair<ll, ll>, ll> m;
  ll cnt = 0LL;
  ll cnto = 0LL;
  ll cntb = 0LL;
  for (ll i = 0LL; i < N; i++) {
    ll A, B;
    cin >> A >> B;
    if (A == 0LL && B != 0LL) {
      cnt++;
      continue;
    }
    if (A == 0LL && B == 0LL) {
      cnto++;
      continue;
    }
    if (A != 0LL && B == 0LL) {
      cntb++;
      continue;
    } else {
      int t = 1;
      if ((double)B / A < 0)
        t = -1;
      pair<ll, ll> p = make_pair(t * abs(B) / gcd(abs(A), abs(B)),
                                 abs(A) / gcd(abs(A), abs(B)));
      m[p]++;
    }
  }
  for (auto x : m) {
    ll a = x.first.first;
    ll b = x.first.second;
    pair<ll, ll> p = make_pair(-b * abs(a) / a, abs(a));
    m[p];
  }
  ll ans = 1LL;
  for (auto x : m) {
    ll A = x.first.first;
    ll B = x.first.second;
    pair<ll, ll> p = make_pair(-B * abs(A) / A, abs(A));
    ll a = x.second;
    ll &b = m[p];
    ans *= (pow(2LL, a, mod) + pow(2LL, b, mod) - 1LL) % mod;
    ans %= mod;
    m[x.first] = 0LL;
    b = 0LL;
  }
  ans *= (pow(2LL, cnt, mod) + pow(2LL, cntb, mod) - 1LL) % mod;
  ans %= mod;
  ans += cnto;
  ans %= mod;
  if (ans < 0LL)
    ans += mod;
  cout << ans - 1LL << endl;
}