Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long int lli;
typedef vector<lli> vlli;
typedef vector<vlli> vvlli;
typedef vector<string> vs;
typedef vector<bool> vb;

#define rep(i, n) for (i = 0; i < (n); ++i)
#define repA(i, a, n) for (i = a; i <= (n); ++i)
#define repD(i, a, n) for (i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sortr(x) sort(x.begin(), x.end(), greater<int>())
#define mod 1000000007
#define ff first
#define ss second
#define fill(a, c) memset(a, c, sizeof(a))
#define formap(i, v) for (auto &i : v)
#define dbg(x) cout << #x << " = " << x << '\n'
#define dbg2(x, y)                                                             \
  cout << #x << " = " << x << " & " << #y << " = " << y << endl;
#define dbgstl(v)                                                              \
  cout << #v << " = \n";                                                       \
  {                                                                            \
    int c = 0;                                                                 \
    formap(it, v) cout << "    " << #v << "[" << ++c << "] = " << it << endl;  \
  }                                                                            \
  cerr << endl
#define dbgstlp(v)                                                             \
  cout << #v << " = \n";                                                       \
  {                                                                            \
    int c = 0;                                                                 \
    formap(it, v) cout << "    " << #v << "[" << ++c << "] = " << it.first     \
                       << " , " << it.second << endl;                          \
  }                                                                            \
  cerr << endl
#define PI 3.14159265358979323846
#define pb push_back

bool comp(const long long int &a, const long long int &b) {
  return abs(a) >= abs(b);
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  lli i, j, k, n, ans = 1, cnt = 0;
  cin >> n >> k;
  vlli a(n);
  rep(i, n) cin >> a[i];
  sort(all(a), comp);

  for (i = 0; i < k; i++) {
    if (a[i] < 0)
      cnt++;
    ans *= a[i];
    ans = ans % mod;
  }

  if (cnt % 2 != 0) {
    lli np, sp, nn, sn;
    np = sp = nn = sn = -1;

    for (i = k; i < n; i++) {
      if (a[i] >= 0 && np == -1)
        np = a[i];
      if (a[i] <= 0 && nn == -1)
        nn = abs(a[i]);
    }

    for (i = k - 1; i >= 0; i--) {
      if (a[i] >= 0 && sp == -1)
        sp = a[i];
      if (a[i] <= 0 && sn == -1)
        sn = abs(a[i]);
    }

    if ((np == -1 || sn == -1) && (nn == -1 || sp == -1)) {
      ans = 1;
      for (i = n - 1; i >= n - k; i--) {
        ans *= a[i];
        ans = ans % mod;
      }
      if (ans < 0)
        ans = (ans + mod) % mod;
    } else if (np == -1 || sn == -1) {
      int f = 0;
      ans = -nn;
      for (i = 0; i < k; i++) {
        if (f == 0 && a[i] == sp) {
          f = 1;
          continue;
        }
        ans *= a[i];
        ans = ans % mod;
      }
    } else if (nn == -1 || sp == -1) {
      int f = 0;
      ans = np;
      for (i = 0; i < k; i++) {
        if (f == 0 && a[i] == -sn) {
          f = 1;
          continue;
        }
        ans *= a[i];
        ans = ans % mod;
      }
    } else {
      if (np * sp >= nn * sn) {
        int f = 0;
        ans = np;
        for (i = 0; i < k; i++) {
          if (f == 0 && a[i] == -sn) {
            f = 1;
            continue;
          }
          ans *= a[i];
          ans = ans % mod;
        }
      } else {
        int f = 0;
        ans = -nn;
        for (i = 0; i < k; i++) {
          if (f == 0 && a[i] == sp) {
            f = 1;
            continue;
          }
          ans *= a[i];
          ans = ans % mod;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long int lli;
typedef vector<lli> vlli;
typedef vector<vlli> vvlli;
typedef vector<string> vs;
typedef vector<bool> vb;

#define rep(i, n) for (i = 0; i < (n); ++i)
#define repA(i, a, n) for (i = a; i <= (n); ++i)
#define repD(i, a, n) for (i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sortr(x) sort(x.begin(), x.end(), greater<int>())
#define mod 1000000007
#define ff first
#define ss second
#define fill(a, c) memset(a, c, sizeof(a))
#define formap(i, v) for (auto &i : v)
#define dbg(x) cout << #x << " = " << x << '\n'
#define dbg2(x, y)                                                             \
  cout << #x << " = " << x << " & " << #y << " = " << y << endl;
#define dbgstl(v)                                                              \
  cout << #v << " = \n";                                                       \
  {                                                                            \
    int c = 0;                                                                 \
    formap(it, v) cout << "    " << #v << "[" << ++c << "] = " << it << endl;  \
  }                                                                            \
  cerr << endl
#define dbgstlp(v)                                                             \
  cout << #v << " = \n";                                                       \
  {                                                                            \
    int c = 0;                                                                 \
    formap(it, v) cout << "    " << #v << "[" << ++c << "] = " << it.first     \
                       << " , " << it.second << endl;                          \
  }                                                                            \
  cerr << endl
#define PI 3.14159265358979323846
#define pb push_back

bool comp(const long long int &a, const long long int &b) {
  return abs(a) > abs(b);
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  lli i, j, k, n, ans = 1, cnt = 0;
  cin >> n >> k;
  vlli a(n);
  rep(i, n) cin >> a[i];
  sort(all(a), comp);

  for (i = 0; i < k; i++) {
    if (a[i] < 0)
      cnt++;
    ans *= a[i];
    ans = ans % mod;
  }

  if (cnt % 2 != 0) {
    lli np, sp, nn, sn;
    np = sp = nn = sn = -1;

    for (i = k; i < n; i++) {
      if (a[i] >= 0 && np == -1)
        np = a[i];
      if (a[i] <= 0 && nn == -1)
        nn = abs(a[i]);
    }

    for (i = k - 1; i >= 0; i--) {
      if (a[i] >= 0 && sp == -1)
        sp = a[i];
      if (a[i] <= 0 && sn == -1)
        sn = abs(a[i]);
    }

    if ((np == -1 || sn == -1) && (nn == -1 || sp == -1)) {
      ans = 1;
      for (i = n - 1; i >= n - k; i--) {
        ans *= a[i];
        ans = ans % mod;
      }
      if (ans < 0)
        ans = (ans + mod) % mod;
    } else if (np == -1 || sn == -1) {
      int f = 0;
      ans = -nn;
      for (i = 0; i < k; i++) {
        if (f == 0 && a[i] == sp) {
          f = 1;
          continue;
        }
        ans *= a[i];
        ans = ans % mod;
      }
    } else if (nn == -1 || sp == -1) {
      int f = 0;
      ans = np;
      for (i = 0; i < k; i++) {
        if (f == 0 && a[i] == -sn) {
          f = 1;
          continue;
        }
        ans *= a[i];
        ans = ans % mod;
      }
    } else {
      if (np * sp >= nn * sn) {
        int f = 0;
        ans = np;
        for (i = 0; i < k; i++) {
          if (f == 0 && a[i] == -sn) {
            f = 1;
            continue;
          }
          ans *= a[i];
          ans = ans % mod;
        }
      } else {
        int f = 0;
        ans = -nn;
        for (i = 0; i < k; i++) {
          if (f == 0 && a[i] == sp) {
            f = 1;
            continue;
          }
          ans *= a[i];
          ans = ans % mod;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}