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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  // a^tを求める
  // 例：d[i] = mint(k/i).pow(n);
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll n;
  cin >> n;
  map<P, ll> mp;
  // vector<P> A;
  rep(i, n) {
    ll a1, b1;
    cin >> a1 >> b1;

    if (a1 == 0 and b1 == 0) {
      mp[make_pair(0, 0)] += 1;
      //  A.push_back(make_pair(0,0));
    } else if (a1 != 0 and b1 == 0) {
      mp[make_pair(1, 0)] += 1;
      //  A.push_back(make_pair(1,0));
    }

    else if (a1 == 0 and b1 != 0) {
      mp[make_pair(0, 1)] += 1;
      //    A.push_back(make_pair(0,1));
    }

    else {
      ll g1 = gcd(a1, b1);
      ll g2 = a1 / g1;
      ll g3 = b1 / g1;
      //  cout<<a1/g1<<' '<<b1/g1<<"gcd"<<endl;
      //    A.push_back(make_pair(a1/g1,b1/g1));
      if (g2 < 0) {
        g2 = -g2;
        g3 = -g3;
      }
      mp[make_pair(g2, g3)] += 1;
    }
  }
  mint ans = 1;
  ll temp = mp[make_pair(0, 0)]; // 0,0を覗く
  // cout<<temp<<endl;
  ll nnew = n - temp;
  mp[make_pair(0, 0)] = 0;
  // cout<<nnew<<endl;

  for (auto i = mp.begin(); i != mp.end(); i++) {
    P now = i->first;
    ll now1 = now.first;
    ll now2 = now.second;
    ll new1, new2;
    ll count = i->second; // 自分
    if (now2 < 0) {
      new1 = -now2;
      new2 = now1;
    }
    if (now2 >= 0) {
      new1 = now2;
      new2 = -now1;
    }
    ll cntnew = mp[make_pair(new1, new2)]; // ペアの数
    // cout<<cntnew<<' '<<now1<<' '<<now2<<' '<<count<<' '<<"cnew"<<endl;
    if (cntnew == 0)
      continue;
    mint a1 = mint(2).pow(count);
    mint a2 = mint(2).pow(cntnew);
    a1 += a2;
    a1 -= 1;
    ans *= a1;
    nnew -= (count + cntnew);
    mp[make_pair(new1, new2)] = 0;
    mp[make_pair(now1, now2)] = 0;
  }

  // cout<<nnew;

  mint a3 = mint(2).pow(nnew);
  ans *= a3;
  ans -= 1;
  ans += temp;
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  // a^tを求める
  // 例：d[i] = mint(k/i).pow(n);
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll n;
  cin >> n;
  map<P, ll> mp;
  // vector<P> A;
  rep(i, n) {
    ll a1, b1;
    cin >> a1 >> b1;
    if (a1 < 0) {
      a1 = -a1;
      b1 = -b1;
    }
    if (a1 == 0 and b1 == 0) {
      mp[make_pair(0, 0)] += 1;
      //  A.push_back(make_pair(0,0));
    } else if (a1 != 0 and b1 == 0) {
      mp[make_pair(1, 0)] += 1;
      //  A.push_back(make_pair(1,0));
    }

    else if (a1 == 0 and b1 != 0) {
      mp[make_pair(0, 1)] += 1;
      //    A.push_back(make_pair(0,1));
    }

    else {
      ll g1 = gcd(a1, b1);
      mp[make_pair(a1 / g1, b1 / g1)] += 1;
      //    A.push_back(make_pair(a1/g1,b1/g1));
    }
  }
  mint ans = 1;
  ll temp = mp[make_pair(0, 0)]; // 0,0を覗く
  // cout<<temp<<endl;
  ll nnew = n - temp;
  mp[make_pair(0, 0)] = 0;
  for (auto i = mp.begin(); i != mp.end(); i++) {
    P now = i->first;
    ll now1 = now.first;
    ll now2 = now.second;
    ll new1, new2;
    ll count = i->second;
    if (now2 < 0) {
      new1 = -now2;
      new2 = now1;
    }
    if (now2 >= 0) {
      new1 = now2;
      new2 = -now1;
    }
    ll cntnew = mp[make_pair(new1, new2)];
    // cout<<cntnew<<now1<<' '<<now2<<' '<<"cnew"<<endl;
    if (cntnew == 0)
      continue;
    mint a1 = mint(2).pow(count);
    mint a2 = mint(2).pow(cntnew);
    a1 += a2;
    a1 -= 1;
    ans *= a1;
    nnew -= (count + cntnew);
  }
  // cout<<nnew;

  mint a3 = mint(2).pow(nnew);
  ans *= a3;
  ans -= 1;
  ans += temp;
  cout << ans;
}