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

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, k, x, y;

// moddivとnCk
ll moddiv(ll a, ll b) {
  ll x0 = MOD, x1 = b, x2, n0 = 0LL, n1 = 1LL, n2, t = a % b, m, ans;
  if (t == 0LL)
    return a / b;
  for (int i = 0; i < 900; i++) {
    m = x0 / x1;
    x2 = x0 - x1 * m;
    n2 = (n0 - m * n1) % MOD;
    if (x2 == 1LL) {
      ans = (n2 + MOD) % MOD;
      break;
    }
    x0 = x1;
    x1 = x2;
    n0 = n1;
    n1 = n2;
  }
  return (a + ((t * ans) % MOD) * b - t) / b;
}
// 剰余の割ざん

// aのn乗 mod c
ll expmod(ll a, ll n) {
  ll ans = 1LL, aa = a, beki = n;
  for (int i = 0; i < 64; i++) {
    if (beki % 2 == 1LL)
      ans = ans * aa % MOD;
    aa = aa * aa % MOD;
    beki /= 2LL;
    if (beki == 0LL)
      break;
  }
  return ans;
}

// nCk
vi mulmod(500001); // 0!,1!,2!,3!,4!,,,,
void first_nCk() {
  mulmod[0] = 1LL;
  for (ll i = 1; i < 500001; i++) {
    mulmod[i] = mulmod[i - 1] * i % MOD;
  }
}
ll nCk(ll nn, ll kk) {
  return moddiv(mulmod[nn], mulmod[kk] * mulmod[nn - kk] % MOD);
}

int main() {
  ll ans = 1;
  cin >> n >> k;

  ll plcnt = 0;
  ll mncnt = 0;
  vi a(n);
  REP(i, n) {
    cin >> a[i];
    if (a[i] > 0)
      plcnt++;
    if (a[i] < 0)
      mncnt++;
  }
  vi mn(mncnt / 2 + 1, 0), pl(plcnt / 2 + 1, 0);

  sort(a.begin(), a.end()); // 低い順
  if (n == k) {
    for (ll i = 0; i < n; i++) {
      ans = ans * a[i];
      ans = (ans % MOD + MOD) % MOD;
    }
    cout << ans << endl;
    return 0;
  }

  if (n == mncnt) {
    if (k % 2 == 1) {
      for (ll i = 0; i < k; i++) {
        ans = ans * a[n - 1 - i];
        ans = (ans % MOD + MOD) % MOD;
      }
      cout << ans << endl;
      return 0;
    } else {
      for (ll i = 0; i < k; i++) {
        ans = ans * a[i];
        ans = (ans % MOD + MOD) % MOD;
      }
      cout << ans << endl;
      return 0;
    }
  }

  if (plcnt + mncnt < k) {
    cout << 0 << endl;
    return 0;
  }

  if (plcnt + mncnt == k) {
    if (mncnt % 2 == 1) {
      cout << 0 << endl;
      return 0;
    } else {

      for (ll i = 0; i < n; i++) {
        if (a[i] == 0)
          continue;
        ans = ans * a[i];
        ans = (ans % MOD + MOD) % MOD;
      }
      cout << ans << endl;
      return 0;
    }
  }

  // 1こしかなければ
  if (k == 1) {
    cout << a[n - 1] << endl;
    return 0;
  }

  // プラス側から
  for (ll i = 0; i < plcnt / 2; i++) {
    pl[i] = a[n - 1 - i * 2] * a[n - 2 - i * 2];
  }
  // マイナス側から
  for (ll i = 0; i < mncnt / 2; i++) {
    mn[i] = a[i * 2] * a[i * 2 + 1];
  }

  ll wpl = 0;
  ll wmn = 0;
  ll mx;
  for (ll i = 0; i < k / 2; i++) {
    if (wpl >= plcnt / 2) {
      mx = mn[wmn];
      wmn++;
    } else {

      if (wmn >= mncnt / 2) {
        mx = pl[wpl];
        wpl++;
      } else {

        if (pl[wpl] > mn[wmn]) {
          mx = pl[wpl];
          wpl++;
        } else {
          mx = mn[wmn];
          wmn++;
        }
      }
    }

    ans = ans * (mx % MOD) % MOD;
  }

  if (k % 2 == 1) {
    if (plcnt > wpl * 2) {
      if (wpl == 0) {
        ans = ans * a[n - 1 - wpl * 2] % MOD;
      } else {
        if (mn[wmn] > a[n - 1 - wpl * 2] * a[n - 1 - wpl * 2 + 1]) {
          ans = moddiv(ans, a[n - 1 - wpl * 2 + 1]);
          ans = ans * (mn[wmn] % MOD) % MOD;
        } else {
          ans = ans * a[n - 1 - wpl * 2] % MOD;
        }
      }

    } else {
      if (plcnt == 0) {
        cout << 0 << endl;
        return 0;
      } else {
        ans = moddiv(ans, a[n - 1 - wpl * 2 + 1]);
        ans = ans * mn[wmn] % MOD;
      }
    }
  }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, k, x, y;

// moddivとnCk
ll moddiv(ll a, ll b) {
  ll x0 = MOD, x1 = b, x2, n0 = 0LL, n1 = 1LL, n2, t = a % b, m, ans;
  if (t == 0LL)
    return a / b;
  for (int i = 0; i < 900; i++) {
    m = x0 / x1;
    x2 = x0 - x1 * m;
    n2 = (n0 - m * n1) % MOD;
    if (x2 == 1LL) {
      ans = (n2 + MOD) % MOD;
      break;
    }
    x0 = x1;
    x1 = x2;
    n0 = n1;
    n1 = n2;
  }
  return (a + ((t * ans) % MOD) * b - t) / b;
}
// 剰余の割ざん

// aのn乗 mod c
ll expmod(ll a, ll n) {
  ll ans = 1LL, aa = a, beki = n;
  for (int i = 0; i < 64; i++) {
    if (beki % 2 == 1LL)
      ans = ans * aa % MOD;
    aa = aa * aa % MOD;
    beki /= 2LL;
    if (beki == 0LL)
      break;
  }
  return ans;
}

// nCk
vi mulmod(500001); // 0!,1!,2!,3!,4!,,,,
void first_nCk() {
  mulmod[0] = 1LL;
  for (ll i = 1; i < 500001; i++) {
    mulmod[i] = mulmod[i - 1] * i % MOD;
  }
}
ll nCk(ll nn, ll kk) {
  return moddiv(mulmod[nn], mulmod[kk] * mulmod[nn - kk] % MOD);
}

int main() {
  ll ans = 1;
  cin >> n >> k;

  ll plcnt = 0;
  ll mncnt = 0;
  vi a(n);
  REP(i, n) {
    cin >> a[i];
    if (a[i] > 0)
      plcnt++;
    if (a[i] < 0)
      mncnt++;
  }
  vi mn(mncnt / 2 + 1, 0), pl(plcnt / 2 + 1, 0);

  sort(a.begin(), a.end()); // 低い順
  if (n == k) {
    for (ll i = 0; i < n; i++) {
      ans = ans * a[i];
      ans = (ans % MOD + MOD) % MOD;
    }
    cout << ans << endl;
    return 0;
  }

  if (n == mncnt) {
    if (k % 2 == 1) {
      for (ll i = 0; i < k; i++) {
        ans = ans * a[n - 1 - i];
        ans = (ans % MOD + MOD) % MOD;
      }
      cout << ans << endl;
      return 0;
    } else {
      for (ll i = 0; i < k; i++) {
        ans = ans * a[i];
        ans = (ans % MOD + MOD) % MOD;
      }
      cout << ans << endl;
      return 0;
    }
  }

  if (plcnt + mncnt < k) {
    cout << 0 << endl;
    return 0;
  }

  if (plcnt + mncnt == k) {
    if (mncnt % 2 == 1) {
      cout << 0 << endl;
      return 0;
    } else {

      for (ll i = 0; i < n; i++) {
        if (a[i] == 0)
          continue;
        ans = ans * a[i];
        ans = (ans % MOD + MOD) % MOD;
      }
      cout << ans << endl;
      return 0;
    }
  }

  // 1こしかなければ
  if (k == 1) {
    cout << a[n - 1] << endl;
    return 0;
  }

  // プラス側から
  for (ll i = 0; i < plcnt / 2; i++) {
    pl[i] = a[n - 1 - i * 2] * a[n - 2 - i * 2];
  }
  // マイナス側から
  for (ll i = 0; i < mncnt / 2; i++) {
    mn[i] = a[i * 2] * a[i * 2 + 1];
  }

  ll wpl = 0;
  ll wmn = 0;
  ll mx;
  for (ll i = 0; i < k / 2; i++) {
    if (wpl >= plcnt / 2) {
      mx = mn[wmn];
      wmn++;
    } else {

      if (wmn >= mncnt / 2) {
        mx = pl[wpl];
        wpl++;
      } else {

        if (pl[wpl] > mn[wmn]) {
          mx = pl[wpl];
          wpl++;
        } else {
          mx = mn[wmn];
          wmn++;
        }
      }
    }

    ans = ans * (mx % MOD) % MOD;
  }

  if (k % 2 == 1) {
    if (plcnt > wpl * 2) {

      if (mn[wmn] > a[n - 1 - wpl * 2] * a[n - 1 - wpl * 2 + 1]) {
        ans = moddiv(ans, a[n - 1 - wpl * 2 + 1]);
        ans = ans * (mn[wmn] % MOD) % MOD;
      } else {
        ans = ans * a[n - 1 - wpl * 2] % MOD;
      }

    } else {
      if (plcnt == 0) {
        cout << 0 << endl;
      } else {
        ans = moddiv(ans, a[n - 1 - wpl * 2 + 1]);
        ans = ans * mn[wmn] % MOD;
      }

      return 0;
    }
  }

  cout << ans << endl;
  return 0;
}