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

#include <bits/stdc++.h>

using namespace std;
#define _rep(n, a, b) for (ll n = (a); n <= (b); ++n)
#define _rev(n, a, b) for (ll n = (a); n >= (b); --n)
#define _for(n, a, b) for (ll n = (a); n < (b); ++n)
#define _rof(n, a, b) for (ll n = (a); n > (b); --n)
#define oo 0x3f3f3f3f3f3f
#define ll long long
#define db long double
#define eps 1e-3
#define bin(x) cout << bitset<10>(x) << endl;
#define what_is(x) cerr << #x << " is " << x << endl
#define met(a, b) memset(a, b, sizeof(a))
#define all(x) x.begin(), x.end()
#define pii pair<ll, ll>
#define pdd pair<db, db>
#define endl "\\n"
const ll mod = 1e9 + 7;
const ll maxn = 2e5 + 10;
deque<ll> a, pos, neg;
db posd[maxn], negd[maxn];

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k, zero = 0;
  cin >> n >> k;

  _rep(i, 1, n) {
    ll x;
    cin >> x;
    if (x < 0) {
      neg.push_back(-x);
    } else if (x > 0) {
      pos.push_back(x);
    } else
      zero++;
  }

  sort(all(pos), greater<ll>());
  sort(all(neg), greater<ll>());

  ll res = 1;
  if ((k & 1) <= pos.size() && (neg.size() / 2 * 2) + pos.size() >= k) {
    // 找正数
    pos.push_front(1);
    neg.push_front(1);

    _for(i, 1, pos.size()) { posd[i] = posd[i - 1] + log(pos[i]); }
    _for(i, 1, neg.size()) { negd[i] = negd[i - 1] + log(neg[i]); }
    int ind = 0;
    db ans = 0;
    for (int i = 0; i < neg.size() && i <= k; i += 2) {
      if (k - i < pos.size() && posd[k - i] + negd[i] > ans) {
        ans = posd[k - i] + negd[i];
        ind = i;
      }
    }
    _rep(i, 1, ind) { res = (res * neg[i]) % mod; }
    _rep(i, 1, k - ind) { res = (res * pos[i]) % mod; }
    res = res % mod;
  } else if (zero) {
    res = 0;
  } else {
    sort(all(neg), [](ll a, ll b) { return abs(a) < abs(b); });
    sort(all(pos));

    pos.push_front(1);
    neg.push_front(1);
    _for(i, 1, pos.size()) { posd[i] = posd[i - 1] + log(pos[i]); }
    _for(i, 1, neg.size()) { negd[i] = negd[i - 1] + log(neg[i]); }
    db mn = oo;
    int ind = 0;
    for (int i = 1; i <= k && i < neg.size(); i += 2) {
      if (k - i >= 0 && k - i < pos.size() && mn > posd[k - i] + negd[i]) {
        mn = posd[k - i] + negd[i];
        ind = i;
      }
    }
    _rep(i, 1, ind) {
      res = res * (-neg[i]) % mod;
      res = (res + mod) % mod;
    }
    _rep(i, 1, k - ind) { res = (res * pos[i] % mod + mod) % mod; }
  }
  cout << (res + mod) % mod << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define _rep(n, a, b) for (ll n = (a); n <= (b); ++n)
#define _rev(n, a, b) for (ll n = (a); n >= (b); --n)
#define _for(n, a, b) for (ll n = (a); n < (b); ++n)
#define _rof(n, a, b) for (ll n = (a); n > (b); --n)
#define oo 0x3f3f3f3f3f3f
#define ll long long
#define db long double
#define eps 1e-3
#define bin(x) cout << bitset<10>(x) << endl;
#define what_is(x) cerr << #x << " is " << x << endl
#define met(a, b) memset(a, b, sizeof(a))
#define all(x) x.begin(), x.end()
#define pii pair<ll, ll>
#define pdd pair<db, db>
#define endl "\\n"
const ll mod = 1e9 + 7;
const ll maxn = 2e5 + 10;
deque<ll> a, pos, neg;
db posd[maxn], negd[maxn];

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k, zero = 0;
  cin >> n >> k;

  _rep(i, 1, n) {
    ll x;
    cin >> x;
    if (x < 0) {
      neg.push_back(x);
    } else if (x > 0) {
      pos.push_back(x);
    } else
      zero++;
  }

  sort(all(pos), greater<ll>());
  sort(all(neg), greater<ll>());

  ll res = 1;
  if ((k & 1) <= pos.size() && (neg.size() / 2 * 2) + pos.size() >= k) {
    // 找正数
    pos.push_front(1);
    neg.push_front(1);

    _for(i, 1, pos.size()) { posd[i] = posd[i - 1] + log(pos[i]); }
    _for(i, 1, neg.size()) { negd[i] = negd[i - 1] + log(-neg[i]); }
    int ind = 0;
    db ans = 0;
    for (int i = 0; i < neg.size() && i <= k; i += 2) {
      if (k - i < pos.size() && posd[k - i] + negd[i] > ans) {
        ans = posd[k - i] + negd[i];
        ind = i;
      }
    }
    _rep(i, 1, ind) { res = (res * neg[i]) % mod; }
    _rep(i, 1, k - ind) { res = (res * pos[i]) % mod; }
    res = res % mod;
  } else if (zero) {
    res = 0;
  } else {

    sort(all(neg));
    _for(i, 0, k) { res = res * neg[i] % mod; }
    res = (mod + res) % mod;
  }
  cout << (res + mod) % mod << endl;
}