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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = (long long)(k); i < (long long)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define PQ(T) priority_queue<T>
#define PQS(T) priority_queue<T, vector<T>, greater<T>>
#define deci cout << fixed << setprecision(15);
typedef long long ll;
const ll inf = 1020304050607080910;
const int mod = 1000000007;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
template <class T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
ll gcd(ll x, ll y) {
  if (x > y)
    swap(x, y);
  while (x != 0) {
    y %= x;
    swap(x, y);
  }
  return y;
}
ll mpow(ll x, ll r) {
  if (r == 0)
    return 1;
  if (r % 2 == 1) {
    return x * mpow(x, r - 1) % mod;
  } else {
    ll aa = mpow(x, r / 2);
    return aa * aa % mod;
  }
}

ll tw[300000];

int main() {
  int N;
  cin >> N;
  ll A[N], B[N], s = 0;
  tw[0] = 1;
  for (int i = 1; i < 300000; i++)
    tw[i] = (tw[i - 1] * 2) % mod;
  vector<PP> arr;
  rep(i, N) {
    int ff;
    cin >> A[i] >> B[i];
    if (A[i] == 0 && B[i] == 0) {
      s++;
      continue;
    }
    if (A[i] == 0) {
      if (B[i] < 0)
        ff = 2;
      else
        ff = 0;
      arr.pb({{0, 1}, ff});
      continue;
    }
    if (B[i] == 0) {
      if (A[i] < 0)
        ff = 1;
      else
        ff = 3;
      arr.pb({{0, 1}, ff});
      continue;
    }
    if (A[i] < 0) {
      if (B[i] < 0)
        ff = 2;
      else {
        ff = 1;
        swap(A[i], B[i]);
      }
    } else {
      if (B[i] < 0) {
        ff = 3;
        swap(A[i], B[i]);
      } else
        ff = 0;
    }
    if (A[i] < 0)
      A[i] = -A[i];
    if (B[i] < 0)
      B[i] = -B[i];
    ll g = gcd(A[i], B[i]);
    A[i] /= g;
    B[i] /= g;
    arr.pb({{A[i], B[i]}, ff});
  }
  sort(all(arr));
  /*	rep(i, arr.size()){
                  cout << arr[i].first.first << " " << arr[i].first.second << "
     " << arr[i].second << endl;
          }
  */
  ll ans = 1;
  P now;
  vector<ll> c(4, 0);
  rep(i, arr.size() + 1) {
    if (i == arr.size()) {
      ll cnt = 1;
      rep(x, 4) cnt = (cnt + tw[c[x]] + mod - 1) % mod;
      rep(x, 2) cnt =
          (cnt + (tw[c[x]] + mod - 1) * (tw[c[x + 2]] - 1) % mod) % mod;
      ans = (ans * cnt) % mod;
      break;
    }
    int ff = arr[i].second;
    if (i == 0) {
      now = arr[i].first;
      c[ff]++;
    } else {
      if (now != arr[i].first) {
        ll cnt = 1;
        rep(x, 4) cnt = (cnt + tw[c[x]] + mod - 1) % mod;
        rep(x, 2) cnt =
            (cnt + (tw[c[x]] + mod - 1) * (tw[c[x + 2]] + mod - 1) % mod) % mod;
        ans = (ans * cnt) % mod;
        now = arr[i].first;
        rep(y, 4) c[y] = 0;
        c[ff]++;
      } else {
        c[ff]++;
      }
    }
  }
  cout << (ans + s + mod - 1) % mod << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = (long long)(k); i < (long long)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define PQ(T) priority_queue<T>
#define PQS(T) priority_queue<T, vector<T>, greater<T>>
#define deci cout << fixed << setprecision(15);
typedef long long ll;
const ll inf = 1020304050607080910;
const int mod = 1000000007;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
template <class T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
ll gcd(ll x, ll y) {
  if (x > y)
    swap(x, y);
  while (x != 0) {
    y %= x;
    swap(x, y);
  }
  return y;
}
ll mpow(ll x, ll r) {
  if (r == 0)
    return 1;
  if (r % 2 == 1) {
    return x * mpow(x, r - 1) % mod;
  } else {
    ll aa = mpow(x, r / 2);
    return aa * aa % mod;
  }
}

ll tw[300000];

int main() {
  int N;
  cin >> N;
  ll A[N], B[N], s = 0;
  tw[0] = 1;
  for (int i = 1; i < 300000; i++)
    tw[i] = (tw[i - 1] * 2) % mod;
  vector<PP> arr;
  rep(i, N) {
    int ff;
    cin >> A[i] >> B[i];
    if (A[i] == 0 && B[i] == 0) {
      s++;
      continue;
    }
    if (A[i] == 0) {
      if (B[i] < 0)
        ff = 2;
      else
        ff = 0;
      arr.pb({{0, 1}, ff});
      continue;
    }
    if (B[i] == 0) {
      if (A[i] < 0)
        ff = 1;
      else
        ff = 3;
      arr.pb({{0, 1}, ff});
      continue;
    }
    if (A[i] < 0) {
      if (B[i] < 0)
        ff = 2;
      else {
        ff = 1;
        swap(A[i], B[i]);
      }
    } else {
      if (B[i] < 0) {
        ff = 3;
        swap(A[i], B[i]);
      } else
        ff = 0;
    }
    if (A[i] < 0)
      A[i] = -A[i];
    if (B[i] < 0)
      B[i] = -B[i];
    ll g = gcd(A[i], B[i]);
    A[i] /= g;
    B[i] /= g;
    arr.pb({{A[i], B[i]}, ff});
  }
  sort(all(arr));
  /*	rep(i, arr.size()){
                  cout << arr[i].first.first << " " << arr[i].first.second << "
     " << arr[i].second << endl;
          }
  */
  ll ans = 1;
  P now;
  vector<ll> c(4, 0);
  rep(i, arr.size() + 1) {
    int ff = arr[i].second;
    if (i == arr.size()) {
      ll cnt = 1;
      rep(x, 4) cnt = (cnt + tw[c[x]] + mod - 1) % mod;
      rep(x, 2) cnt =
          (cnt + (tw[c[x]] + mod - 1) * (tw[c[x + 2]] - 1) % mod) % mod;
      ans = (ans * cnt) % mod;
      break;
    }
    if (i == 0) {
      now = arr[i].first;
      c[ff]++;
    } else {
      if (now != arr[i].first) {
        ll cnt = 1;
        rep(x, 4) cnt = (cnt + tw[c[x]] + mod - 1) % mod;
        rep(x, 2) cnt =
            (cnt + (tw[c[x]] + mod - 1) * (tw[c[x + 2]] + mod - 1) % mod) % mod;
        ans = (ans * cnt) % mod;
        now = arr[i].first;
        rep(y, 4) c[y] = 0;
        c[ff]++;
      } else {
        c[ff]++;
      }
    }
  }
  cout << (ans + s + mod - 1) % mod << endl;
  return 0;
}