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

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#line 1 "lib/modint.hpp"

#include <iostream>

// Modint
struct Mint {
  static const long long mod = (long long)1e9 + 7;
  long long val;

  Mint() { val = 0; }
  Mint(long long a) {
    val = a;
    verify_value();
  }

  void verify_value() {
    if (val >= mod)
      val %= mod;
    if (val < 0)
      val %= mod, val += mod;
  }

  Mint pow(long long p) const {
    Mint cur = Mint(val), ret = 1;
    while (p > 0) {
      if (p & 1)
        ret *= cur;
      cur *= cur;
      p >>= 1LL;
    }
    return ret;
  }
  Mint inv() const {
    if (val == 0)
      std::cerr << "WARNING: inv() is called with 0." << std::endl;
    return pow(mod - 2);
  }

  Mint operator+() const { return *this; }
  Mint operator-() const { return Mint(mod - val); }

  Mint operator+=(const Mint &a) {
    val += a.val;
    if (val >= mod)
      val -= mod;
    return Mint(val);
  }
  Mint operator*=(const Mint &a) {
    val *= a.val;
    if (val >= mod)
      val %= mod;
    return Mint(val);
  }
  Mint operator-=(const Mint &a) { return *this += -a; }
  Mint operator/=(const Mint &a) { return *this *= a.inv(); }

  Mint operator++() { return *this += Mint(1); }
  Mint operator--() { return *this -= Mint(1); }
  Mint operator++(int) {
    Mint ret = *this;
    ++(*this);
    return ret;
  }
  Mint operator--(int) {
    Mint ret = *this;
    --(*this);
    return ret;
  }

  operator long long() const { return val; }
};

Mint operator+(const Mint &a, const Mint &b) {
  long long ret = a.val + b.val;
  if (ret >= Mint::mod)
    ret -= Mint::mod;
  return Mint(ret);
}
Mint operator*(const Mint &a, const Mint &b) {
  long long ret = a.val * b.val;
  if (ret >= Mint::mod)
    ret %= Mint::mod;
  return Mint(ret);
}
Mint operator-(const Mint &a, const Mint &b) { return a + (-b); }
Mint operator/(const Mint &a, const Mint &b) { return a * b.inv(); }

std::ostream &operator<<(std::ostream &out, const Mint &a) {
  return out << a.val;
}
std::istream &operator>>(std::istream &in, Mint &a) {
  in >> a.val;
  a.verify_value();
  return in;
}

Mint pow(Mint a, long long b) { return a.pow(b); }

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

int main() {
  int n;
  std::cin >> n;
  std::vector<long long> a(n), b(n);
  Mint cntzero = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i] >> b[i];
    if (a[i] < 0)
      a[i] *= -1, b[i] *= -1;
    long long d = gcd(a[i], std::abs(b[i]));
    if (a[i] == 0 && b[i] == 0)
      cntzero++;
    else {
      a[i] /= d, b[i] /= d;
      if (a[i] == 0)
        b[i] = 1;
      if (b[i] == 0)
        a[i] = 1;
    }
  }
  std::map<std::pair<long long, long long>, long long> cnt;
  for (int i = 0; i < n; i++) {
    cnt[{a[i], b[i]}]++;
  }
  Mint ans = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] == 0 && b[i] == 0)
      continue;
    if (cnt[{a[i], b[i]}] == 0)
      continue;
    long long c, d;
    if (a[i] == 0 || b[i] == 0)
      c = b[i], d = a[i];
    else {
      if (b[i] >= 0)
        c = b[i], d = -a[i];
      else
        c = -b[i], d = a[i];
    }
    Mint k = Mint(1);
    if (cnt[{a[i], b[i]}] > 0)
      k += pow(Mint(2), cnt[{a[i], b[i]}]) - Mint(1);
    if (cnt[{c, d}] > 0)
      k += pow(Mint(2), cnt[{c, d}]) - Mint(1);
    ans *= k;
    cnt[{a[i], b[i]}] = 0;
    cnt[{c, d}] = 0;
  }
  std::cout << ans - Mint(1) + cntzero << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#line 1 "lib/modint.hpp"

#include <iostream>

// Modint
struct Mint {
  static const long long mod = (long long)1e9 + 7;
  long long val;

  Mint() { val = 0; }
  Mint(long long a) {
    val = a;
    verify_value();
  }

  void verify_value() {
    if (val >= mod)
      val %= mod;
    if (val < 0)
      val %= mod, val += mod;
  }

  Mint pow(long long p) const {
    Mint cur = Mint(val), ret = 1;
    while (p > 0) {
      if (p & 1)
        ret *= cur;
      cur *= cur;
      p >>= 1LL;
    }
    return ret;
  }
  Mint inv() const {
    if (val == 0)
      std::cerr << "WARNING: inv() is called with 0." << std::endl;
    return pow(mod - 2);
  }

  Mint operator+() const { return *this; }
  Mint operator-() const { return Mint(mod - val); }

  Mint operator+=(const Mint &a) {
    val += a.val;
    if (val >= mod)
      val -= mod;
    return Mint(val);
  }
  Mint operator*=(const Mint &a) {
    val *= a.val;
    if (val >= mod)
      val %= mod;
    return Mint(val);
  }
  Mint operator-=(const Mint &a) { return *this += -a; }
  Mint operator/=(const Mint &a) { return *this *= a.inv(); }

  Mint operator++() { return *this += Mint(1); }
  Mint operator--() { return *this -= Mint(1); }
  Mint operator++(int) {
    Mint ret = *this;
    ++(*this);
    return ret;
  }
  Mint operator--(int) {
    Mint ret = *this;
    --(*this);
    return ret;
  }

  operator long long() const { return val; }
};

Mint operator+(const Mint &a, const Mint &b) {
  long long ret = a.val + b.val;
  if (ret >= Mint::mod)
    ret -= Mint::mod;
  return Mint(ret);
}
Mint operator*(const Mint &a, const Mint &b) {
  long long ret = a.val * b.val;
  if (ret >= Mint::mod)
    ret %= Mint::mod;
  return Mint(ret);
}
Mint operator-(const Mint &a, const Mint &b) { return a + (-b); }
Mint operator/(const Mint &a, const Mint &b) { return a * b.inv(); }

std::ostream &operator<<(std::ostream &out, const Mint &a) {
  return out << a.val;
}
std::istream &operator>>(std::istream &in, Mint &a) {
  in >> a.val;
  a.verify_value();
  return in;
}

Mint pow(Mint a, long long b) { return a.pow(b); }

#line 1 "lib/union_find.hpp"

#include <vector>

class UnionFind {
public:
  UnionFind(int n) : par(n, -1), ran(n, -1) {}

  void unite(int x, int y) {
    x = find_root(x);
    y = find_root(y);
    if (x == y)
      return;

    if (ran[x] < ran[y]) {
      par[find_root(y)] += par[find_root(x)];
      par[x] = y;
    } else {
      par[find_root(x)] += par[find_root(y)];
      par[y] = x;
      if (ran[x] == ran[y])
        ran[x]++;
    }
  }

  int size(int x) { return -par[find_root(x)]; }

  bool same(int x, int y) { return find_root(x) == find_root(y); }

  int find_root(int x) {
    if (par[x] < 0) {
      return x;
    } else {
      return par[x] = find_root(par[x]);
    }
  }

private:
  std::vector<int> par, ran;
};

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

int main() {
  int n;
  std::cin >> n;
  std::vector<long long> a(n), b(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i] >> b[i];
    if (a[i] < 0)
      a[i] *= -1, b[i] *= -1;
    long long d = gcd(a[i], std::abs(b[i]));
    assert(d != 0);
    a[i] /= d;
    b[i] /= d;
  }
  std::map<std::pair<long long, long long>, long long> cnt;
  for (int i = 0; i < n; i++) {
    cnt[{a[i], b[i]}]++;
  }
  Mint ans = 1;
  for (int i = 0; i < n; i++) {
    if (cnt[{a[i], b[i]}] == 0)
      continue;
    long long c, d;
    if (b[i] >= 0)
      c = b[i], d = -a[i];
    else
      c = -b[i], d = a[i];
    Mint k = Mint(1);
    if (cnt[{a[i], b[i]}] > 0)
      k += pow(Mint(2), cnt[{a[i], b[i]}]) - Mint(1);
    if (cnt[{c, d}] > 0)
      k += pow(Mint(2), cnt[{c, d}]) - Mint(1);
    ans *= k;
    cnt[{a[i], b[i]}] = 0;
    cnt[{c, d}] = 0;
  }
  std::cout << ans - Mint(1) << std::endl;
  return 0;
}