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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const LL mod = 1000000007;
typedef pair<LL, LL> pa;
LL gcd(LL a, LL b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
int R(LL x) {
  if (x < 0)
    return -1;
  if (x == 0)
    return 0;
  return 1;
}
LL pow(LL x, LL p) {
  LL ret = 1ll;
  while (p) {
    if (p & 1)
      ret *= x, ret %= mod;
    p /= 2;
    x *= x;
    x %= mod;
  }
  return ret;
}
map<pa, int> mp;
map<pa, bool> bk;
LL a[N], b[N], ax[N], bx[N], ans, n, tot;
LL st1[N], st2[N], cnt, cnt00, cnta0, cntb0;
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &a[i], &b[i]);
    int flag = R(a[i]) * R(b[i]);
    if (flag == 0) {
      ax[i] = 0;
      bx[i] = 0;
      if (a[i] == 0 && b[i] == 0)
        cnt00++;
      else if (a[i] == 0)
        cnta0++;
      else
        cntb0++;
      continue;
    }
    ax[i] = abs(a[i]);
    bx[i] = abs(b[i]);
    LL tmp = gcd(ax[i], bx[i]);
    ax[i] /= tmp;
    bx[i] /= tmp;
    pa now = make_pair(ax[i] * flag, bx[i]);
    mp[now]++;
  }
  for (int i = 1; i <= n; i++) {
    if (ax[i] == 0)
      continue;
    if (!bk[make_pair(ax[i] * R(a[i]) * R(b[i]), bx[i])]) {
      bk[make_pair(ax[i] * R(a[i]) * R(b[i]), bx[i])] = 1;
      bk[make_pair(bx[i] * R(a[i]) * R(b[i]) * (-1), ax[i])] = 1;
      LL tmp1 = 1ll * mp[make_pair(bx[i] * R(a[i]) * R(b[i]) * (-1), ax[i])];
      LL tmp2 = 1ll * mp[make_pair(ax[i] * R(a[i]) * R(b[i]), bx[i])];
      if (tmp1 == 0)
        continue;
      st1[++tot] = tmp1;
      st2[tot] = tmp2;
      cnt += tmp1 + tmp2;
    }
  }
  cnt += cnta0 + cntb0 + cnt00;
  if (cnta0 == 0)
    cnt -= cntb0;
  else if (cntb0 == 0)
    cnt -= cnta0;
  else {
    st1[++tot] = cnta0, st2[tot] = cntb0;
  }
  ans = pow(2ll, n - cnt) % mod;
  for (int i = 1; i <= tot; i++) {
    // cout<<st1[i]<<" "<<st2[i]<<endl;
    LL tmp = (pow(2ll, st1[i]) % mod + pow(2ll, st2[i]) % mod - 1) % mod;
    ans *= tmp;
    ans %= mod;
  }
  // if(cnt0)ans*=2;ans%=mod;
  ans = ans - 1 + cnt00;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const LL mod = 1000000007;
typedef pair<LL, LL> pa;
LL gcd(LL a, LL b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
int R(LL x) {
  if (x < 0)
    return -1;
  if (x == 0)
    return 0;
  return 1;
}
LL pow(int x, LL p) {
  LL ret = 1;
  while (p) {
    if (p & 1)
      ret *= x, ret %= mod;
    p /= 2;
    x *= x;
    x %= mod;
  }
  return ret;
}
map<pa, int> mp;
map<pa, bool> bk;
LL a[N], b[N], ax[N], bx[N], ans, n, tot;
LL st1[N], st2[N], cnt;
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &a[i], &b[i]);
    int flag = R(a[i]) * R(b[i]);
    ax[i] = abs(a[i]);
    bx[i] = abs(b[i]);
    LL tmp = gcd(ax[i], bx[i]);
    ax[i] /= tmp;
    bx[i] /= tmp;
    pa now = make_pair(ax[i] * flag, bx[i]);
    mp[now]++;
  }
  for (int i = 1; i <= n;
       i++) { // cout<<ax[i]*R(a[i])*R(b[i])<<" "<<bx[i]<<endl;
    if (!bk[make_pair(ax[i] * R(a[i]) * R(b[i]), bx[i])]) {
      bk[make_pair(ax[i] * R(a[i]) * R(b[i]), bx[i])] = 1;
      bk[make_pair(bx[i] * R(a[i]) * R(b[i]) * (-1), ax[i])] = 1;
      LL tmp1 = 1ll * mp[make_pair(bx[i] * R(a[i]) * R(b[i]) * (-1), ax[i])];
      LL tmp2 = 1ll * mp[make_pair(ax[i] * R(a[i]) * R(b[i]), bx[i])];
      if (tmp1 == 0)
        continue;
      st1[++tot] = tmp1;
      st2[tot] = tmp2;
      cnt += tmp1 + tmp2;
    }
  }
  ans = pow(2, n - cnt);
  for (int i = 1; i <= tot; i++) {
    // cout<<st1[i]<<" "<<st2[i]<<endl;
    LL tmp = (pow(2, st1[i]) + pow(2, st2[i]) - 1) % mod;
    ans *= tmp;
    ans %= mod;
  }
  cout << ans - 1 << endl;
}