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

#include <algorithm>
#include <cstdio>
#include <vector>

using u128 = __uint128_t;
using u64 = unsigned long long int;
using s64 = long long int;
using u32 = unsigned int;

constexpr u32 mod = 1000000007;

int n;

u32 pow2(int n) {
  if (n == 0)
    return 1;
  u64 r = 2;
  u32 x = 1U << (31 - __builtin_clz(n));
  while (x != 1) {
    r = r * r % mod;
    x >>= 1;
    if (n & x) {
      r <<= 1;
      if (r >= mod)
        r -= mod;
    }
  }
  return r;
}

int main() {

  scanf("%d", &n);

  using quot = std::pair<u64, u64>;
  std::vector<quot> vp, vn;
  int zero2 = 0;

  for (int i = 0; i < n; i++) {
    s64 a, b;
    scanf("%lld%lld", &a, &b);
    if (a == 0 && b == 0)
      zero2++;
    else if (b != 0 && (a == 0 || (a ^ b) >= 0)) {
      vp.emplace_back(std::abs(a), std::abs(b));
    } else {
      vn.emplace_back(std::abs(b), std::abs(a));
    }
  }

  auto comp = [](const quot &a, const quot &b) {
    return (u128)a.first * b.second < (u128)b.first * a.second;
  };

  std::sort(vp.begin(), vp.end(), comp);
  std::sort(vn.begin(), vn.end(), comp);

  auto equiv = [](const quot &a, const quot &b) {
    return (u128)a.first * b.second == (u128)b.first * a.second;
  };

  std::vector<std::pair<quot, u32>> vpp, vnn;
  quot prev = {1, -1};
  for (auto const &x : vp) {
    if (equiv(x, prev))
      vpp.back().second++;
    else {
      vpp.emplace_back(quot(x.first, x.second), 1);
      prev = x;
    }
  }
  prev = {1, -1};
  for (auto const &x : vn) {
    if (equiv(x, prev))
      vnn.back().second++;
    else {
      vnn.emplace_back(quot(x.first, x.second), 1);
      prev = x;
    }
  }

  u64 ans = 1;
  auto ip = vpp.begin(), in = vnn.begin();
  while (ip != vpp.end() && in != vnn.end()) {
    if (equiv(ip->first, in->first)) {
      ans = ans * (pow2(ip->second) + pow2(in->second) + mod - 1) % mod;
      ++ip, ++in;
    } else if (comp(ip->first, in->first)) {
      ans = ans * pow2(ip->second) % mod;
      ++ip;
    } else {
      ans = ans * pow2(in->second) % mod;
      ++in;
    }
  }
  while (in != vnn.end()) {
    ans = ans * pow2(in->second) % mod;
    ++in;
  }
  while (ip != vpp.end()) {
    ans = ans * pow2(ip->second) % mod;
    ++ip;
  }

  ans = (ans + zero2 + mod - 1) % mod;
  printf("%llu\\n", ans);

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using u128 = __uint128_t;
using u64 = unsigned long long int;
using s64 = long long int;
using u32 = unsigned int;

constexpr u32 mod = 1000000007;

int n;

u32 pow2(int n) {
  u64 r = 2;
  u32 x = 1U << (31 - __builtin_clz(n));
  while (x != 1) {
    r = r * r % mod;
    x >>= 1;
    if (n & x) {
      r <<= 1;
      if (r >= mod)
        r -= mod;
    }
  }
  return r;
}

int main() {

  scanf("%d", &n);

  using quot = std::pair<u64, u64>;
  std::vector<quot> vp, vn;

  for (int i = 0; i < n; i++) {
    s64 a, b;
    scanf("%lld%lld", &a, &b);
    if ((a ^ b) >= 0) {
      vp.emplace_back(std::abs(a), std::abs(b));
    } else {
      vn.emplace_back(std::abs(b), std::abs(a));
    }
  }

  auto comp = [](const quot &a, const quot &b) {
    return (u128)a.first * b.second < (u128)b.first * a.second;
  };

  std::sort(vp.begin(), vp.end(), comp);
  std::sort(vn.begin(), vn.end(), comp);

  auto equiv = [](const quot &a, const quot &b) {
    return (u128)a.first * b.second == (u128)b.first * a.second;
  };

  std::vector<std::pair<quot, u32>> vpp, vnn;
  quot prev = vp.front();
  vpp.emplace_back(vp.front(), 0);
  for (auto const &x : vp) {
    if (equiv(x, prev))
      vpp.back().second++;
    else {
      vpp.emplace_back(quot(x.first, x.second), 1);
      prev = x;
    }
  }
  prev = vn.front();
  vnn.emplace_back(vn.front(), 0);
  for (auto const &x : vn) {
    if (equiv(x, prev))
      vnn.back().second++;
    else {
      vnn.emplace_back(quot(x.first, x.second), 1);
      prev = x;
    }
  }

  u64 ans = 1;
  auto ip = vpp.begin(), in = vnn.begin();
  while (ip != vpp.end() && in != vnn.end()) {
    if (equiv(ip->first, in->first)) {
      ans = ans * (pow2(ip->second) + pow2(in->second) + mod - 1) % mod;
      ++ip, ++in;
    } else if (comp(ip->first, in->first)) {
      ans = ans * pow2(ip->second) % mod;
      ++ip;
    } else {
      ans = ans * pow2(in->second) % mod;
      ++in;
    }
  }
  while (in != vnn.end()) {
    ans = ans * pow2(in->second) % mod;
    ++in;
  }
  while (ip != vpp.end()) {
    ans = ans * pow2(ip->second) % mod;
    ++ip;
  }

  ans = (ans + mod - 1) % mod;
  printf("%llu\\n", ans);

  return 0;
}