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

// This code was made by Chinese_zjc_.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
#define INF 0x3fffffffffffffff
using namespace std;
int n, a[200001], b[200001];

int gcd(int __A, int __B) { return __A % __B ? gcd(__B, __A % __B) : __B; }

struct Fraction {
  int molecule, denominator;

  void reduction() {
    if (molecule == 0) {
      denominator = 1;
      return;
    }
    int GCD = gcd(molecule, denominator);
    molecule /= GCD;
    denominator /= GCD;
    if (denominator < 0) {
      molecule = -molecule;
      denominator = -denominator;
    }
  }

  Fraction operator-() const {
    Fraction tmp = *this;
    tmp.molecule = -tmp.molecule;
    return tmp;
  }

  Fraction reciprocal() const {
    Fraction tmp;
    tmp.molecule = denominator;
    tmp.denominator = molecule;
    tmp.reduction();
    return tmp;
  }

  bool operator<(const Fraction x) const {
    return molecule == x.molecule ? denominator < x.denominator
                                  : molecule < x.molecule;
  }
} k[200002];
map<Fraction, int> l;
int cnt, s[200002], ans, t[200002], p[200002], _00, _0x, _x0;
#define MOD 1000000007
int C(int a, int b) {
  //	cout<<t[a]<<' '<<p[b]<<' '<<p[a-b]<<endl;
  return t[a] * p[b] % MOD * p[a - b] % MOD;
}
int power(int a, int b, int c) {
  int tmp = 1;
  while (b) {
    if (b & 1) {
      tmp = (tmp * a) % c;
    }
    b >>= 1;
    a = (a * a) % c;
  }
  return tmp;
}
signed main() {
  std::ios::sync_with_stdio(false);
  //	freopen("a.in","r",stdin);
  //	freopen("std.out","w",stdout);
  cin >> n;
  t[0] = 1;
  p[0] = 1;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i];
    t[i] = t[i - 1] * i % MOD;
    p[i] = power(t[i], MOD - 2, MOD);
    if (a[i] == 0 && b[i] == 0) {
      ++_00;
      continue;
    }
    if (a[i] == 0) {
      ++_0x;
      continue;
    }
    if (b[i] == 0) {
      ++_x0;
      continue;
    }
    k[i].molecule = a[i];
    k[i].denominator = b[i];
    k[i].reduction();
    ++l[k[i]];
  }
  s[++cnt] = 1;
  for (int i = 1; i <= _0x; ++i) {
    s[cnt] = (s[cnt] + C(_0x, i)) % MOD;
  }
  for (int i = 1; i <= _x0; ++i) {
    s[cnt] = (s[cnt] + C(_x0, i)) % MOD;
  }
  for (int i = 1; i <= n; ++i) {
    if (l[k[i]]) {
      Fraction tmp = -k[i].reciprocal();
      s[++cnt] = 1;
      int N = l[k[i]];
      for (int j = 1; j <= N; ++j) {
        s[cnt] = (s[cnt] + C(N, j)) % MOD;
      }
      N = l[tmp];
      for (int j = 1; j <= N; ++j) {
        s[cnt] = (s[cnt] + C(N, j)) % MOD;
      }
      l[k[i]] = l[tmp] = 0;
    }
  }
  ans = 1;
  for (int i = 1; i <= cnt; ++i) {
    ans = (ans * s[i]) % MOD;
  }
  cout << (ans + MOD - 1 + _00) % MOD << endl;
  return 0;
}


Error: 0

Fixed submission:

// This code was made by Chinese_zjc_.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
#define INF 0x3fffffffffffffff
using namespace std;
int n, a[200001], b[200001];

int gcd(int __A, int __B) { return __A % __B ? gcd(__B, __A % __B) : __B; }

struct Fraction {
  int molecule, denominator;

  Fraction() { denominator = 1; }

  void read(int a, int b) {
    molecule = a;
    denominator = b;
    reduction();
  }

  void reduction() {
    if (molecule == 0) {
      denominator = 1;
      return;
    }
    int GCD = gcd(molecule, denominator);
    molecule /= GCD;
    denominator /= GCD;
    if (denominator < 0) {
      molecule = -molecule;
      denominator = -denominator;
    }
  }

  friend void reduction_of_fractions_to_a_common_denominator(Fraction &a,
                                                             Fraction &b) {
    a.reduction();
    b.reduction();
    int GCD = gcd(a.denominator, b.denominator);
    a.molecule *= b.denominator / GCD;
    b.molecule *= a.denominator / GCD;
    a.denominator = b.denominator = a.denominator * b.denominator / GCD;
  }

  Fraction operator-() const {
    Fraction tmp = *this;
    tmp.molecule = -tmp.molecule;
    return tmp;
  }

  Fraction reciprocal() const {
    Fraction tmp;
    tmp.molecule = denominator;
    tmp.denominator = molecule;
    tmp.reduction();
    return tmp;
  }

  Fraction operator+(const Fraction x) const {
    Fraction tmp, a = *this, b = x;
    reduction_of_fractions_to_a_common_denominator(a, b);
    tmp.molecule = a.molecule + b.molecule;
    tmp.denominator = a.denominator;
    tmp.reduction();
    return tmp;
  }

  Fraction operator-(const Fraction x) const { return *this + (-x); }

  Fraction operator*(const Fraction x) const {
    Fraction tmp;
    tmp.molecule = molecule * x.molecule;
    tmp.denominator = denominator * x.denominator;
    tmp.reduction();
    return tmp;
  }

  Fraction operator/(const Fraction x) const {
    return *this * (x.reciprocal());
  }

  bool operator==(const Fraction x) const {
    Fraction a = *this, b = x;
    reduction_of_fractions_to_a_common_denominator(a, b);
    if (a.molecule == b.molecule) {
      return true;
    }
    return false;
  }

  bool operator<(const Fraction x) const {
    Fraction a = *this, b = x;
    reduction_of_fractions_to_a_common_denominator(a, b);
    if (a.molecule < b.molecule) {
      return true;
    }
    return false;
  }

  bool operator>(const Fraction x) const {
    Fraction a = *this, b = x;
    reduction_of_fractions_to_a_common_denominator(a, b);
    if (a.molecule > b.molecule) {
      return true;
    }
    return false;
  }

  bool operator!=(const Fraction x) const {
    Fraction a = *this, b = x;
    reduction_of_fractions_to_a_common_denominator(a, b);
    if (a.molecule != b.molecule) {
      return true;
    }
    return false;
  }

  bool operator<=(const Fraction x) const {
    Fraction a = *this, b = x;
    reduction_of_fractions_to_a_common_denominator(a, b);
    if (a.molecule <= b.molecule) {
      return true;
    }
    return false;
  }

  bool operator>=(const Fraction x) const {
    Fraction a = *this, b = x;
    reduction_of_fractions_to_a_common_denominator(a, b);
    if (a.molecule >= b.molecule) {
      return true;
    }
    return false;
  }

  Fraction be(const int x) {
    Fraction tmp;
    tmp.molecule = x;
    tmp.denominator = 1;
    return tmp;
  }

  Fraction be(const double x, const int l) {
    Fraction tmp;
    tmp.denominator = 1;
    for (int i = 1; i <= l; ++i) {
      tmp.denominator *= 10;
    }
    tmp.molecule = x * tmp.denominator;
    return tmp;
  }
} k[200001];
map<Fraction, int> l;
int cnt, s[200001], ans, t[200001], p[200001];
#define MOD 1000000007
int C(int a, int b) {
  //	cout<<t[a]<<' '<<p[b]<<' '<<p[a-b]<<endl;
  return t[a] * p[b] % MOD * p[a - b] % MOD;
}
int pow(int a, int b, int c) {
  int tmp = 1;
  while (b) {
    if (b & 1) {
      tmp = (tmp * a) % c;
    }
    b >>= 1;
    a = (a * a) % c;
  }
  return tmp;
}
signed main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  t[0] = 1;
  p[0] = 1;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i];
    k[i].molecule = a[i];
    k[i].denominator = b[i];
    k[i].reduction();
    ++l[k[i]];
    t[i] = t[i - 1] * i % MOD;
    p[i] = pow(t[i], MOD - 2, MOD);
  }
  for (int i = 1; i <= n; ++i) {
    if (l[k[i]]) {
      Fraction tmp = -k[i].reciprocal();
      s[++cnt] = 1;
      for (int j = 1; j <= l[k[i]]; ++j) {
        s[cnt] = (s[cnt] + C(l[k[i]], j)) % MOD;
      }
      for (int j = 1; j <= l[tmp]; ++j) {
        s[cnt] = (s[cnt] + C(l[tmp], j)) % MOD;
      }
      //			cout<<k[i].molecule<<'/'<<k[i].denominator<<'
      //'<<tmp.molecule<<'/'<<tmp.denominator<<' '<<s[cnt]<<endl;
      l[k[i]] = l[tmp] = 0;
    }
  }
  ans = 1;
  for (int i = 1; i <= cnt; ++i) {
    ans = (ans * s[i]) % MOD;
  }
  cout << (ans + MOD - 1) % MOD << endl;
  return 0;
}