Problem p02679 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (lint)(n); i++)
#define REP(i, n) for (int i = 1; i <= (lint)(n); i++)
#define all(V) V.begin(), V.end()
typedef long long lint;
typedef std::pair<lint, lint> P;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = 1e-9;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
inline lint gcd(lint a, lint b) {
  while (b) {
    lint c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
lint mypow(lint a, lint b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  lint memo = mypow(a, b >> 1);
  return memo * memo;
}
lint modpow(lint a, lint b, lint m) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  lint memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
template <typename T> void printArray(std::vector<T> &vec) {
  rep(i, vec.size() - 1) std::cout << vec[i] << " ";
  std::cout << vec.back() << std::endl;
}
template <typename T> void printArray(T l, T r) {
  T rprev = r;
  rprev--;
  for (T i = l; i != rprev; i++) {
    std::cout << *i << " ";
  }
  std::cout << *rprev << std::endl;
}
std::string to_string(std::vector<int> &vec) {
  std::string res = "[";
  rep(i, vec.size() - 1) res += std::to_string(vec[i]) + ", ";
  res += std::to_string(vec.back()) + "]";
  return res;
}
class Rational {
public:
  lint num, den;
  Rational() : num(0), den(1) {}
  Rational(lint p) : num(p), den(1) {}
  Rational(lint p, lint q) : num(p), den(q) { reduce(); }
  void reduce() {
    if (den < 0) {
      num = -num;
      den = -den;
    }
    if (num == 0)
      den = 1;
    else {
      lint r = gcd(std::abs(num), std::abs(den));
      num /= r;
      den /= r;
    }
  }
  inline bool operator<(const Rational &rhs) const {
    return num * rhs.den < rhs.num * den;
  }
  inline bool operator==(const Rational &rhs) const {
    return num == rhs.num && den == rhs.den;
  }
  inline bool operator>(const Rational &rhs) const {
    return num * rhs.den > rhs.num * den;
  }
  inline Rational &operator+=(Rational rhs) {
    lint r = lcm(this->den, rhs.den);
    num *= r / this->den;
    rhs.num *= r / rhs.den;
    den = rhs.den = r;
    num += rhs.num;
    reduce();
    return *this;
  }
  inline Rational operator-() { return Rational(-num, den); }
  inline Rational &operator-=(Rational rhs) {
    lint r = lcm(this->den, rhs.den);
    num *= r / this->den;
    rhs.num *= r / rhs.den;
    den = rhs.den = r;
    num -= rhs.num;
    reduce();
    return *this;
  }
  inline Rational &operator*=(const Rational &rhs) {
    num *= rhs.num;
    den *= rhs.den;
    reduce();
    return *this;
  }
  inline Rational &operator/=(const Rational &rhs) {
    num *= rhs.den;
    den *= rhs.num;
    reduce();
    return *this;
  }
  template <typename T> Rational operator+(const T &rhs) const {
    return Rational(*this) += rhs;
  }
  template <typename T> Rational &operator+=(const T &rhs) {
    return operator+=(Rational(rhs));
  }
  template <typename T> Rational operator-(const T &rhs) const {
    return Rational(*this) -= rhs;
  }
  template <typename T> Rational &operator-=(const T &rhs) {
    return operator-=(Rational(rhs));
  }
  template <typename T> Rational operator*(const T &rhs) const {
    return Rational(*this) *= rhs;
  }
  template <typename T> Rational &operator*=(const T &rhs) {
    return operator*=(Rational(rhs));
  }
  template <typename T> Rational operator/(const T &rhs) const {
    return Rational(*this) /= rhs;
  }
  template <typename T> Rational &operator/=(const T &rhs) {
    return operator/=(Rational(rhs));
  }

  Rational inv() const { return Rational(den, num); }
};
std::ostream &operator<<(std::ostream &ofs, const Rational &x) {
  ofs << x.num << "/" << x.den;
  return ofs;
}
template <unsigned int modulo = 1000000007> class ModInt {
  lint value;

public:
  ModInt() : value(0) {}
  template <typename T> ModInt(T value = 0) : value(value) {
    if (value < 0)
      value = -(lint)(-value % modulo) + modulo;
    this->value = value % modulo;
  }
  inline operator int() const { return value; }
  inline ModInt &operator+=(const ModInt &x) {
    value += x.value;
    if (value >= modulo)
      value -= modulo;
    return *this;
  }
  inline ModInt &operator++() {
    if (value == modulo - 1)
      value = 0;
    else
      value++;
    return *this;
  }
  inline ModInt operator-() const { return ModInt(0) -= *this; }
  inline ModInt &operator-=(const ModInt &x) {
    value -= x.value;
    if (value < 0)
      value += modulo;
    return *this;
  }
  inline ModInt &operator--() {
    if (value == 0)
      value = modulo - 1;
    else
      value--;
    return *this;
  }
  inline ModInt &operator*=(const ModInt &x) {
    value = value * x.value % modulo;
    return *this;
  }
  inline ModInt &operator/=(ModInt rhs) {
    int exp = modulo - 2;
    while (exp) {
      if (exp & 1)
        *this *= rhs;
      rhs *= rhs;
      exp >>= 1;
    }
    return *this;
  }
  template <typename T> ModInt operator+(const T &rhs) const {
    return ModInt(*this) += rhs;
  }
  template <typename T> ModInt &operator+=(const T &rhs) {
    return operator+=(ModInt(rhs));
  }
  template <typename T> ModInt operator-(const T &rhs) const {
    return ModInt(*this) -= rhs;
  }
  template <typename T> ModInt &operator-=(const T &rhs) {
    return operator-=(ModInt(rhs));
  }
  template <typename T> ModInt operator*(const T &rhs) const {
    return ModInt(*this) *= rhs;
  }
  template <typename T> ModInt &operator*=(const T &rhs) {
    return operator*=(ModInt(rhs));
  }
  template <typename T> ModInt operator/(const T &rhs) const {
    return ModInt(*this) /= rhs;
  }
  template <typename T> ModInt &operator/=(const T &rhs) {
    return operator/=(ModInt(rhs));
  }
};
template <unsigned int T>
std::istream &operator>>(std::istream &ist, ModInt<T> &x) {
  lint a;
  ist >> a;
  x = a;
  return ist;
}
int n;
int a0, b0, both0;
class myhash {
public:
  lint operator()(const Rational &r) const {
    lint seed = 123219831;
    seed ^= r.num + seed + (seed >> 6);
    seed ^= r.den + seed + (seed >> 6);
    return seed;
  }
};
std::unordered_map<Rational, int, myhash> mp;
std::unordered_set<Rational, myhash> d;
int main() {
  std::cin >> n;
  rep(i, n) {
    lint a, b;
    std::cin >> a >> b;
    if (a == 0 && b == 0)
      both0++;
    else if (a == 0)
      a0++;
    else if (b == 0)
      b0++;
    else
      mp[Rational(a, b)]++;
  }
  ModInt<> ans = 1;
  std::vector<Rational> vec;
  for (const auto &i : mp) {
    if (d.find(i.first) != d.end())
      continue;
    vec.emplace_back(i.first);
    d.insert(i.first);
    d.insert(-i.first.inv());
  }
  for (const Rational &i : vec) {
    ans *=
        modpow(2, mp[i], 1000000007) + modpow(2, mp[-i.inv()], 1000000007) - 1;
  }
  ans *= modpow(2, a0, 1000000007) + modpow(2, b0, 1000000007) - 1;
  ans += both0;
  ans -= 1;
  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (lint)(n); i++)
#define REP(i, n) for (int i = 1; i <= (lint)(n); i++)
#define all(V) V.begin(), V.end()
typedef long long lint;
typedef std::pair<lint, lint> P;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = 1e-9;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
inline lint gcd(lint a, lint b) {
  while (b) {
    lint c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
lint mypow(lint a, lint b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  lint memo = mypow(a, b >> 1);
  return memo * memo;
}
lint modpow(lint a, lint b, lint m) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  lint memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
template <typename T> void printArray(std::vector<T> &vec) {
  rep(i, vec.size() - 1) std::cout << vec[i] << " ";
  std::cout << vec.back() << std::endl;
}
template <typename T> void printArray(T l, T r) {
  T rprev = r;
  rprev--;
  for (T i = l; i != rprev; i++) {
    std::cout << *i << " ";
  }
  std::cout << *rprev << std::endl;
}
std::string to_string(std::vector<int> &vec) {
  std::string res = "[";
  rep(i, vec.size() - 1) res += std::to_string(vec[i]) + ", ";
  res += std::to_string(vec.back()) + "]";
  return res;
}
class Rational {
public:
  lint num, den;
  Rational() : num(0), den(1) {}
  Rational(lint p) : num(p), den(1) {}
  Rational(lint p, lint q) : num(p), den(q) { reduce(); }
  void reduce() {
    if (den < 0) {
      num = -num;
      den = -den;
    }
    if (num == 0)
      den = 1;
    else {
      lint r = gcd(std::abs(num), std::abs(den));
      num /= r;
      den /= r;
    }
  }
  inline bool operator<(const Rational &rhs) const {
    return num * rhs.den < rhs.num * den;
  }
  inline bool operator==(const Rational &rhs) const {
    return num == rhs.num && den == rhs.den;
  }
  inline bool operator>(const Rational &rhs) const {
    return num * rhs.den > rhs.num * den;
  }
  inline Rational &operator+=(Rational rhs) {
    lint r = lcm(this->den, rhs.den);
    num *= r / this->den;
    rhs.num *= r / rhs.den;
    den = rhs.den = r;
    num += rhs.num;
    reduce();
    return *this;
  }
  inline Rational operator-() { return Rational(-num, den); }
  inline Rational &operator-=(Rational rhs) {
    lint r = lcm(this->den, rhs.den);
    num *= r / this->den;
    rhs.num *= r / rhs.den;
    den = rhs.den = r;
    num -= rhs.num;
    reduce();
    return *this;
  }
  inline Rational &operator*=(const Rational &rhs) {
    num *= rhs.num;
    den *= rhs.den;
    reduce();
    return *this;
  }
  inline Rational &operator/=(const Rational &rhs) {
    num *= rhs.den;
    den *= rhs.num;
    reduce();
    return *this;
  }
  template <typename T> Rational operator+(const T &rhs) const {
    return Rational(*this) += rhs;
  }
  template <typename T> Rational &operator+=(const T &rhs) {
    return operator+=(Rational(rhs));
  }
  template <typename T> Rational operator-(const T &rhs) const {
    return Rational(*this) -= rhs;
  }
  template <typename T> Rational &operator-=(const T &rhs) {
    return operator-=(Rational(rhs));
  }
  template <typename T> Rational operator*(const T &rhs) const {
    return Rational(*this) *= rhs;
  }
  template <typename T> Rational &operator*=(const T &rhs) {
    return operator*=(Rational(rhs));
  }
  template <typename T> Rational operator/(const T &rhs) const {
    return Rational(*this) /= rhs;
  }
  template <typename T> Rational &operator/=(const T &rhs) {
    return operator/=(Rational(rhs));
  }

  Rational inv() const { return Rational(den, num); }
};
std::ostream &operator<<(std::ostream &ofs, const Rational &x) {
  ofs << x.num << "/" << x.den;
  return ofs;
}
template <unsigned int modulo = 1000000007> class ModInt {
  lint value;

public:
  ModInt() : value(0) {}
  template <typename T> ModInt(T value = 0) : value(value) {
    if (value < 0)
      value = -(lint)(-value % modulo) + modulo;
    this->value = value % modulo;
  }
  inline operator int() const { return value; }
  inline ModInt &operator+=(const ModInt &x) {
    value += x.value;
    if (value >= modulo)
      value -= modulo;
    return *this;
  }
  inline ModInt &operator++() {
    if (value == modulo - 1)
      value = 0;
    else
      value++;
    return *this;
  }
  inline ModInt operator-() const { return ModInt(0) -= *this; }
  inline ModInt &operator-=(const ModInt &x) {
    value -= x.value;
    if (value < 0)
      value += modulo;
    return *this;
  }
  inline ModInt &operator--() {
    if (value == 0)
      value = modulo - 1;
    else
      value--;
    return *this;
  }
  inline ModInt &operator*=(const ModInt &x) {
    value = value * x.value % modulo;
    return *this;
  }
  inline ModInt &operator/=(ModInt rhs) {
    int exp = modulo - 2;
    while (exp) {
      if (exp & 1)
        *this *= rhs;
      rhs *= rhs;
      exp >>= 1;
    }
    return *this;
  }
  template <typename T> ModInt operator+(const T &rhs) const {
    return ModInt(*this) += rhs;
  }
  template <typename T> ModInt &operator+=(const T &rhs) {
    return operator+=(ModInt(rhs));
  }
  template <typename T> ModInt operator-(const T &rhs) const {
    return ModInt(*this) -= rhs;
  }
  template <typename T> ModInt &operator-=(const T &rhs) {
    return operator-=(ModInt(rhs));
  }
  template <typename T> ModInt operator*(const T &rhs) const {
    return ModInt(*this) *= rhs;
  }
  template <typename T> ModInt &operator*=(const T &rhs) {
    return operator*=(ModInt(rhs));
  }
  template <typename T> ModInt operator/(const T &rhs) const {
    return ModInt(*this) /= rhs;
  }
  template <typename T> ModInt &operator/=(const T &rhs) {
    return operator/=(ModInt(rhs));
  }
};
template <unsigned int T>
std::istream &operator>>(std::istream &ist, ModInt<T> &x) {
  lint a;
  ist >> a;
  x = a;
  return ist;
}
int n;
int a0, b0, both0;
class myhash {
public:
  lint operator()(const Rational &r) const {
    return (r.num + (r.num >> 6) + 0x19f7e5) ^
           (r.den + (r.den >> 6) + 0x19f7e5);
  }
};
std::unordered_map<Rational, int, myhash> mp;
std::unordered_set<Rational, myhash> d;
int main() {
  std::cin >> n;
  rep(i, n) {
    lint a, b;
    std::cin >> a >> b;
    if (a == 0 && b == 0)
      both0++;
    else if (a == 0)
      a0++;
    else if (b == 0)
      b0++;
    else
      mp[Rational(a, b)]++;
  }
  ModInt<> ans = 1;
  std::vector<Rational> vec;
  for (const auto &i : mp) {
    if (d.find(i.first) != d.end())
      continue;
    vec.emplace_back(i.first);
    d.insert(i.first);
    d.insert(-i.first.inv());
  }
  for (const Rational &i : vec) {
    ans *=
        modpow(2, mp[i], 1000000007) + modpow(2, mp[-i.inv()], 1000000007) - 1;
  }
  ans *= modpow(2, a0, 1000000007) + modpow(2, b0, 1000000007) - 1;
  ans += both0;
  ans -= 1;
  std::cout << ans << std::endl;
  return 0;
}