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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll mod = 1000000007;

template <typename T> constexpr T modpow(T a, int n) { //(a^n)%MOD
  T ret = 1;
  while (n > 0) {
    if ((n & 1) != 0) { // n%2==1
      ret = ret * a % mod;
    }
    a = a * a % mod;
    n = n / 2;
  }
  return ret;
}

int main() {
  int k, n;
  cin >> n >> k;
  vector<pair<ll, ll>> a(n);
  int pl = 0, mn = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i].second;
    a[i].first = (a[i].second < 0 ? -a[i].second : a[i].second);
    if (a[i].second < 0)
      ++mn;
    else
      ++pl;
  }
  sort(a.rbegin(), a.rend());
  ll ans = 1LL;

  if ((pl == 0 && k % 2 == 1) || (k == n && mn % 2 == 1)) { // ans is minus
    for (int i = 0; i < k; ++i) {
      ans *= a[n - 1 - i].second;
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
    return 0;
  }

  for (int i = 0; i < k; ++i) {
    ans *= a[i].second;
    ans %= mod;
  }

  if (ans >= 0) {
    cout << ans << endl;
    return 0;
  }

  ll tans = 1;
  ll ttans = 1;

  ll t = 1, tt = 1;

  for (int i = k - 1; i >= 0; --i) {
    if (a[i].second > 0) {
      tans *= modpow(a[i].second, mod - 2);
      tans %= mod;
      tt *= a[i].first;

      break;
    }
    if (i == 0) {
      t = 0;
      tans = 0;
    }
  }
  for (int i = k; i < n; ++i) {
    if (a[i].second < 0) {
      tans *= a[i].second;
      tans %= mod;
      t *= a[i].first;
      break;
    }
    if (i == n - 1) {
      t = 0;
      tans = 0;
    }
  }

  for (int i = k - 1; i >= 0; --i) {
    if (a[i].second < 0) {
      ttans *= modpow(a[i].first, mod - 2);
      ttans *= -1;
      ttans %= mod;
      t *= a[i].first;
      break;
    }
    if (i == 0) {
      tt = 0;
      ttans = 0;
    }
  }

  for (int i = k; i < n; ++i) {
    if (a[i].second > 0) {
      ttans *= a[i].second;
      ttans %= mod;
      tt *= a[i].second;
      break;
    }
    if (i == n - 1) {
      tt = 0;
      ttans = 0;
    }
  }
  if (t < tt) {
    cout << (ans * ttans) % mod << endl;
  } else {
    cout << (ans * tans) % mod << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll mod = 1000000007;

template <typename T> constexpr T modpow(T a, int n) { //(a^n)%MOD
  T ret = 1;
  while (n > 0) {
    if ((n & 1) != 0) { // n%2==1
      ret = ret * a % mod;
    }
    a = a * a % mod;
    n = n / 2;
  }
  return ret;
}

int main() {
  int k, n;
  cin >> n >> k;
  vector<pair<ll, ll>> a(n);
  int pl = 0, mn = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i].second;
    a[i].first = (a[i].second < 0 ? -a[i].second : a[i].second);
    if (a[i].second < 0)
      ++mn;
    else
      ++pl;
  }
  sort(a.rbegin(), a.rend());
  ll ans = 1;

  if ((pl == 0 && k % 2 == 1) || (k == n && mn % 2 == 1)) { // ans is minus
    abort();
    for (int i = 0; i < k; ++i) {
      ans *= a[n - 1 - i].second;
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
    return 0;
  }

  for (int i = 0; i < k; ++i) {
    ans *= a[i].second;
    ans %= mod;
  }

  if (ans >= 0) {
    cout << ans << endl;
    return 0;
  }

  ans += mod;

  ll tans = ans;

  for (int i = k - 1; i >= 0; --i) {
    if (a[i].second > 0) {
      tans *= modpow(a[i].second, mod - 2);
      tans %= mod;
      break;
    }
    if (i == 0)
      tans = 0;
  }
  for (int i = k; i < n; ++i) {
    if (a[i].second < 0) {
      tans *= a[i].second;
      tans %= mod;
      break;
    }
    if (i == n - 1)
      tans = 0;
  }

  ll ttans = ans;

  for (int i = k - 1; i >= 0; --i) {
    if (a[i].second < 0) {
      ttans *= modpow(a[i].first, mod - 2);
      ttans %= mod;
      ttans *= -1;

      break;
    }
    if (i == 0)
      ttans = 0;
  }

  for (int i = k; i < n; ++i) {
    if (a[i].second > 0) {
      ttans *= a[i].second;
      ttans %= mod;

      break;
    }
    if (i == n - 1)
      ttans = 0;
  }

  if (tans < 0)
    tans += mod;
  if (ttans < 0)
    ttans += mod;
  cout << max(tans, ttans) << endl;
  return 0;
}