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

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>

long long GCD(long long x, long long y) {
  long long max = std::max(x, y);
  long long min = std::min(x, y);
  if (min == 0) {
    return x;
  } else {
    return GCD(min, max % min);
  }
}

int main() {
  int N;
  scanf("%d", &N);
  std::vector<long long> A(N), B(N);
  std::map<std::pair<long long, long long>, std::pair<int, int>> mp;
  int zero = 0;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
    if (A[i] == 0 && B[i] == 0) {
      zero++;
    } else {
      long long gcd = GCD(abs(A[i]), abs(B[i]));
      if (A[i] < 0) {
        A[i] *= -1;
        B[i] *= -1;
      }
      // printf("gcd=%lld\\n", gcd);
      if (B[i] == 0) {
        mp[std::make_pair(1, 0)].second++;
      } else if (A[i] == 0) {
        mp[std::make_pair(1, 0)].first++;
      } else if (B[i] < 0) {
        mp[std::make_pair(-B[i] / gcd, A[i] / gcd)].second++;
      } else {
        mp[std::make_pair(A[i] / gcd, B[i] / gcd)].first++;
      }
    }
  }
  long long ans = 1;
  std::vector<long long> power2(N + 4);
  power2[0] = 1;
  long long mod = 1e9 + 7;
  for (int i = 1; i < N + 4; i++) {
    power2[i] = power2[i - 1] * 2;
    power2[i] %= mod;
  }

  for (auto &entry : mp) {
    int num1 = entry.second.first;
    int num2 = entry.second.second;
    // printf("x=%lld, y=%lld, num1=%lld, num2=%lld\\n", x,y,num1,num2);
    // printf("num1=%lld, num2=%lld\\n", num1,num2);
    ans *= power2[num1] + power2[num2] - 1;
    if (ans >= mod)
      ans %= mod;
    else if (ans < 0)
      ans += mod;
  }
  ans += zero;
  ans--;
  if (ans >= mod)
    ans %= mod;
  else if (ans < 0)
    ans += mod;
  std::cout << ans << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>

long long GCD(long long x, long long y) {
  long long max = std::max(x, y);
  long long min = std::min(x, y);
  if (min == 0) {
    return x;
  } else {
    return GCD(min, max % min);
  }
}

int main() {
  int N;
  scanf("%d", &N);
  std::vector<long long> A(N), B(N);
  std::map<std::pair<long long, long long>, std::pair<int, int>> mp;
  int zero = 0;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    scanf("%lld", &B[i]);
    if (A[i] == 0 && B[i] == 0) {
      zero++;
    } else {
      long long gcd = GCD(abs(A[i]), abs(B[i]));
      if (A[i] < 0) {
        A[i] *= -1;
        B[i] *= -1;
      }
      if (B[i] <= 0) {
        mp[std::make_pair(-B[i] / gcd, A[i] / gcd)].second++;
      } else {
        mp[std::make_pair(A[i] / gcd, B[i] / gcd)].first++;
      }
    }
  }
  long long ans = 1;
  std::vector<long long> power2(N + 4);
  power2[0] = 1;
  long long mod = 1e9 + 7;
  for (int i = 1; i < N + 4; i++) {
    power2[i] = power2[i - 1] * 2;
    power2[i] %= mod;
  }

  for (auto &entry : mp) {
    long long num1 = entry.second.first;
    long long num2 = entry.second.second;
    // printf("x=%lld, y=%lld, num1=%lld, num2=%lld\\n", x,y,num1,num2);
    // printf("num1=%lld, num2=%lld\\n", num1,num2);
    ans *= power2[num1] + power2[num2] - 1;
    if (ans >= mod)
      ans %= mod;
    else if (ans < 0)
      ans += mod;
  }
  ans += std::min(1, zero);
  ans--;
  if (ans >= mod)
    ans %= mod;
  else if (ans < 0)
    ans += mod;
  std::cout << ans << std::endl;
  return 0;
}