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

#include <bits/stdc++.h>
using namespace std;
// T+N
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
#define endl "\\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <class T, class T2> inline bool chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline bool chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const ll mod = 1e9 + 7;
template <class T> inline void fix(T &x) {
  if (x >= mod || x <= -mod) {
    x %= mod;
  }
  if (x < 0) {
    x += mod;
  }
}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}

struct Frac {
  ll a, b;
  Frac() {}
  Frac(ll _a, ll _b) {
    a = _a;
    b = _b;
    ll g = gcd(abs(a), abs(b));
    a /= g;
    b /= g;
    if (b < 0) {
      a *= -1;
      b *= -1;
    }
  }
  bool operator<(const Frac &other) const {
    if (a == other.a) {
      return b < other.b;
    }
    return a < other.a;
  }
  bool operator==(const Frac &other) const {
    return a == other.a && b == other.b;
  }
  bool operator!=(const Frac &other) const {
    return a != other.a || b != other.b;
  }
};

map<Frac, ll> cnt;

ll fpow(ll x, ll p) {
  if (p == 0) {
    return 1;
  }
  ll ans = fpow(x, p / 2ll);
  ans = (ans * ans) % mod;
  if (p & 1) {
    return (ans * x) % mod;
  } else {
    return ans;
  }
}

const ll MAX_N = 2e5 + 10;
Frac arr[MAX_N];

Frac inv(Frac a) { return Frac(-a.b, a.a); }

ll cnt1, cnt2;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  int bcnt = 0;
  ll cnta = 0, cntb = 0;
  ll fake = 0;
  for (ll i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    if (a == b && b == 0) {
      fake++;
      continue;
    }
    if (a == 0) {
      cnta++;
    } else if (b == 0) {
      cntb++;
    } else {
      arr[bcnt++] = Frac(a, b);
      cnt[arr[bcnt - 1]]++;
    }
  }
  ll ans = 1;
  sort(arr, arr + bcnt);
  for (ll i = 0; i < bcnt; i++) {
    assert(arr[i].a != 0 && arr[i].b != 0);
    assert(arr[i] != inv(arr[i]));
    if (i == 0 || arr[i] != arr[i - 1]) {
      ll now = fpow(2, cnt[arr[i]]) + fpow(2, cnt[inv(arr[i])]) - 1;
      fix(now);
      cnt[arr[i]] = 0;
      cnt[inv(arr[i])] = 0;
      ans = (ans * now) % mod;

      fix(ans);
    }
  }
  ll now = fpow(2, cnta) + fpow(2, cntb) - 1;
  fix(now);
  ans = (ans * now);
  fix(ans);
  ans--;
  fix(ans);
  ans += fake;
  fix(ans);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// T+N
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
#define endl "\\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <class T, class T2> inline bool chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline bool chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const ll mod = 1e9 + 7;
template <class T> inline void fix(T &x) {
  if (x >= mod | x <= -mod) {
    x %= mod;
  }
  if (x < 0) {
    x += mod;
  }
}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

struct Frac {
  ll a, b;
  Frac() {}
  Frac(ll _a, ll _b) {
    a = _a;
    b = _b;
    ll g = __gcd(a, b);
    a /= g;
    b /= g;
    if (b < 0) {
      a *= -1;
      b *= -1;
    }
  }
  bool operator<(const Frac &other) const {
    if (a == other.a) {
      return b < other.b;
    }
    return a < other.a;
  }
  bool operator==(const Frac &other) const {
    return a == other.a && b == other.b;
  }
  bool operator!=(const Frac &other) const {
    return a != other.a || b != other.b;
  }
};

map<Frac, ll> cnt;

ll fpow(ll x, ll p) {
  if (p == 0) {
    return 1;
  }
  ll ans = fpow(x, p / 2);
  ans = (ans * ans) % mod;
  if (p & 1) {
    return (ans * x) % mod;
  } else {
    return ans;
  }
}

const ll MAX_N = 1e5 + 10;
Frac arr[MAX_N];

Frac inv(Frac a) { return Frac(-a.b, a.a); }

ll cnt1, cnt2;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  int bcnt = 0;
  ll cnta = 0, cntb = 0;
  for (ll i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    if (a == b && b == 0) {
      continue;
    }
    if (a == 0) {
      cnta++;
    } else if (b == 0) {
      cntb++;
    } else {
      arr[bcnt++] = Frac(a, b);
      cnt[arr[i]]++;
    }
  }
  ll ans = 1;
  sort(arr, arr + bcnt);
  for (ll i = 0; i < bcnt; i++) {
    if (i == 0 || arr[i] != arr[i - 1]) {
      ll now = fpow(2, cnt[arr[i]]) + fpow(2, cnt[inv(arr[i])]) - 1;
      fix(now);
      cnt[arr[i]] = 0;
      cnt[inv(arr[i])] = 0;
      ans = (ans * now) % mod;
    }
  }
  ll now = fpow(2, cnta) + fpow(2, cntb) - 1;
  fix(now);
  ans--;
  fix(ans);
  cout << ans << endl;
  return 0;
}