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

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

// x^nをmodで計算する関数
ll sisu(ll x, ll n) {
  ll mod = 1e9 + 7;
  ll nv = n;
  vector<ll> bi(40, 0);
  vector<ll> cal(40, 0);
  ll k = 0;
  ll ans = 1;
  while (nv != 0) {
    bi[k] = nv % 2;
    nv /= 2;
    ++k;
  }
  rep(i, 40) {
    if (i == 0) {
      cal[i] = x % mod;
    } else {
      cal[i] = (cal[i - 1] * cal[i - 1]) % mod;
    }
    if (bi[i] == 1) {
      ans *= cal[i];
      ans = ans % mod;
    }
  }

  return ans;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> in(k + 1, 0);
  ll nn;

  vector<vector<ll>> y(k + 1);

  rep(i, k) {
    in[i + 1] = k / (i + 1);
    nn = 2;
    while (nn * (i + 1) <= k) {
      y[nn * (i + 1)].push_back(i + 1);
      ++nn;
    }
  }

  vector<ll> mi(k + 1, 0);
  ll l;
  ll ans = 0;
  ll mod = 1e9 + 7;

  // cout << sisu(in[8],n) << endl;
  // cout << sisu(7,8) << endl;
  // cout << sisu(2,5) << endl;

  ll ko, mi2;

  rep(i, k) {
    nn = k - i;
    // cout << nn << endl;

    l = y[nn].size();
    ko = sisu(in[nn], n);
    mi2 = mi[nn];
    rep(j, l) { mi[y[nn][j]] += (ko - mi2); }
    // cout << in[nn] << " " << n << endl;

    // cout << sisu(1,3) << endl;
    ans += (ko - mi2) * nn;
    // cout << 777 << endl;
    ans = ans % mod;
  }
  while (ans < 0) {
    ans += mod;
  }

  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  ll h, w, m2;
  cin >> h >> w >> m2;
  vector<vector<ll>> m;
  vector<vector<ll>> hh(h, vector<ll>(2, 0));
  vector<vector<ll>> ww(w, vector<ll>(2, 0));
  vector<ll> hh2;
  vector<ll> ww2;

  rep(i, h) { hh[i][1] = i; }
  rep(i, w) { ww[i][1] = i; }

  ll x, y;
  rep(i, m2) {
    cin >> x >> y;
    --x;
    --y;
    ++hh[x][0];
    ++ww[y][0];
    //++m[x][y];
    m.push_back({x, y});
  }

  sort(hh.rbegin(), hh.rend());
  sort(ww.rbegin(), ww.rend());
  sort(m.rbegin(), m.rend());
  sort(hh2.rbegin(), hh2.rend());
  sort(ww2.rbegin(), ww2.rend());

  /*rep(i,h){
      cout << hh[i][0] << hh[i][1] <<  endl;
  }*/

  ll ans = 0;

  /*rep(i,h){
      rep(j,w){
          //cout << i << " " << j << " " << hh[i][0] << " " << ww[j][0] << " "
  << m[hh[i][1]][ww[j][1]] << endl;

          //cout << i << " " << j << " " <<
  hh[i][0]+ww[j][0]-m[hh[i][1]][ww[j][1]] << endl;
          ans=max(ans,hh[i][0]+ww[j][0]-m[hh[i][1]][ww[j][1]]);
          if(ans>(hh[i][0]+ww[j][0]-m[hh[i][1]][ww[j][1]]+1)){
              break;
          }
      }
  }*/

  ll hma, wma;
  hma = hh[0][0];
  wma = ww[0][0];

  rep(i, h) {
    if (hh[i][0] == hma) {
      hh2.push_back(hh[i][1]);
    } else {
      break;
    }
  }
  rep(i, w) {
    if (ww[i][0] == wma) {
      ww2.push_back(ww[i][1]);
    } else {
      break;
    }
  }

  ll hl, wl;
  hl = hh2.size();
  wl = ww2.size();

  int rr = 1;
  ans = hma + wma;
  int k = 0;

  rep(i, hl) {
    rep(j, wl) {
      while (1) {
        if (m[k][0] == hh2[i] and m[k][1] == ww2[j]) {
          break;
        } else {
          ++k;
          if (k == m2) {
            rr = 0;
            break;
          }
        }
      }
      if (rr == 0) {
        break;
      }
    }
    if (rr == 0) {
      break;
    }
  }

  if (rr == 1) {
    --ans;
  }

  cout << ans << endl;

  return 0;
}