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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1e9 + 7;
const long double Pi = acos(-1);
using P = pair<ll, ll>;
map<P, ll> mp;
ll nature(ll a) {
  if (a >= 0) {
    return a;
  } else {
    return -1 * a;
  }
}
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
ll pw(ll x, ll y) {
  ll tmp = x;
  ll res = 1;
  while (y) {
    if (y % 2)
      res = res * tmp % MOD;
    y >>= 1;
    tmp = tmp * tmp % MOD;
  }
  return res;
}
int main() {
  ll N, count0 = 0, rock1 = 0, rock2 = 0;
  cin >> N;
  map<P, ll> visited;
  vector<ll> A(N); // こっちにーも格納//x座標
  vector<ll> B(N); // 必ず正になる//y座標
  for (ll i = 0; i < N; i++) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      A.at(i) = 0;
      B.at(i) = 0;
      count0++;
    } else if (a == 0 && b != 0) {
      A.at(i) = 0;
      B.at(i) = 1;
      rock1++;
    } else if (a != 0 && b == 0) {
      A.at(i) = 1;
      B.at(i) = 0;
      rock2++;
    } else {
      // gcd(nature(a),nature(b))
      if (a > 0 && b > 0) {
        A.at(i) = a / gcd(nature(a), nature(b));
        B.at(i) = b / gcd(nature(a), nature(b));
      }
      if (a < 0 && b > 0) {
        A.at(i) = a / gcd(nature(a), nature(b));
        B.at(i) = b / gcd(nature(a), nature(b));
      }
      if (a > 0 && b < 0) {
        A.at(i) = -1 * a / gcd(nature(a), nature(b));
        B.at(i) = -1 * b / gcd(nature(a), nature(b));
      }
      if (a < 0 && b < 0) {
        A.at(i) = -1 * a / gcd(nature(a), nature(b));
        B.at(i) = -1 * b / gcd(nature(a), nature(b));
      }
    }
    mp[make_pair(A.at(i), B.at(i))]++;
    // if(i==0){cout<<A.at(i)<<" "<<B.at(i)<<endl;}
  }
  ll ans = 1;
  ll n = N;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (n == count0) {
    cout << n << endl;
    return 0;
  }
  // cout<<mp[make_pair(1,2)]<<endl;
  // cout<<rock1<<" "<<rock2<<endl;
  for (ll i = 0; i < N; i++) {
    ll count = 0;
    if (A.at(i) == 0 && B.at(i) == 0) {
    } else if (A.at(i) > 0 && B.at(i) != 0 &&
               visited[make_pair(A.at(i), B.at(i))] != 54) {
      // cout<<i<<endl;
      ll x = mp[make_pair(-1 * B.at(i), A.at(i))];
      ll y = mp[make_pair(A.at(i), B.at(i))];
      // cout<<pw(2,x)+pw(2,y)<<endl;
      if (x > 0 && y > 0) {
        n -= x;
        n -= y;
        visited[make_pair(A.at(i), B.at(i))] = 54;
        count += (pw(2, x) + pw(2, y) - 1);
        if (count % MOD != 0) {
          count = count % MOD;
        } else {
          count = MOD;
        }
      }
    } else if (A.at(i) == 0 && B.at(i) != 0 && visited[make_pair(0, 0)] != 54 &&
               rock1 > 0 && rock2 > 0) {
      count = pw(2, rock1) + pw(2, rock2) - 1;
      n -= rock1;
      n -= rock2;
      count = (count + MOD) % MOD;
      visited[make_pair(0, 0)] = 54;
      // cout<<43220936513<<endl;
    }
    if (count > 0) {
      ans *= count;
    }
    ans %= MOD;
    // if(B.at(i)==2237){cout<<i<<" "<<A.at(i)<<endl;}
    // if(count>0)cout<<i<<" "<<count<<" "<<A.at(i)<<" "<<B.at(i)<<endl;
  }
  // cout<<A.at(0)<<" "<<B.at(0)<<endl;
  // cout<<ans<<" "<<n<<endl;
  if (count0 == 0) {
    ll answer = ans * pw(2, n) - 1;
    cout << (answer + MOD) % MOD << endl;
  } else {
    ll answer = ans * pw(2, n - count0) - 1 + count0;
    cout << (answer + MOD) % MOD << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
const int MOD = 1e9 + 7;
const ld Pi = acos(-1);
int gcd(ll x, ll y) {
  if (x % y == 0) {
    return y;
  } else {
    gcd(y, x % y);
  }
}
int nature(ll z) {
  if (z >= 0) {
    return z;
  } else {
    return -1 * z;
  }
}
ll mod_pow(ll a, ll b) {
  if (b == 0) {
    return 1;
  }
  ll res = mod_pow(a * a % MOD, b / 2);
  if (b & 1)
    res = res * a % MOD;
  return res;
}
int main() {
  ll N, count0 = 0, countx0 = 0, county0 = 0, answer = 0;
  cin >> N;
  vector<ll> vec(N);
  vector<ll> first(N);
  vector<ll> second(N);
  vector<vector<ll>> plus(N, vector<ll>(N));
  vector<vector<ll>> mainasu(N, vector<ll>(N));
  for (ll i = 0; i < N; i++) {
    ll a, b;
    cin >> a >> b;
    a %= MOD;
    b %= MOD;
    ll c = a, d = b;
    if (a == 0 && b == 0) {
      ++count0;
      first.at(i) = 0;
      second.at(i) = 0;
    }
    if (a == 0 && b != 0) {
      ++countx0;
      first.at(i) = 0;
      second.at(i) = b;
    }
    if (a != 0 && b == 0) {
      ++county0;
      first.at(i) = a;
      second.at(i) = 0;
    } else {
      a /= gcd((ll)nature(c), (ll)nature(d));
      b /= gcd((ll)nature(c), (ll)nature(d));
      if (c * d > 0) {
        a = nature(a);
        b = nature(b);
      } else {
        a = -1 * nature(a);
        b = nature(b);
      }
    }
    first.at(i) = a;
    second.at(i) = b;
    if (a >= 1) {
      plus.at(a - 1).at(b - 1)++;
    }
    if (a < 0) {
      mainasu.at(nature(a + 1)).at(b - 1)++;
    }
  }
  for (ll i = 0; i < N; i++) {
    if (first.at(i) > 0) {
      if (mainasu.at(second.at(i) - 1).at(first.at(i) - 1) > 0) {
        answer += mainasu.at(second.at(i) - 1).at(first.at(i) - 1) *
                  (mainasu.at(second.at(i) - 1).at(first.at(i) - 1) + 1) / 2;
      }
    }
    if (first.at(i) < 0) {
      if (plus.at(second.at(i) - 1).at(-1 * first.at(i) - 1) > 0) {
        answer += plus.at(second.at(i) - 1).at(-1 * first.at(i) - 1) *
                  (plus.at(second.at(i) - 1).at(-1 * first.at(i) - 1) + 1) / 2;
      }
    }
  }
}