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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mod 1000000007
#define mo 1000000007
#define mod2 1000000000
#define mod1 998244353
#define lol                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define F(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define ss second

int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans = ans + i * (n + 1 - i);
  }
  for (ll i = 1; i <= n - 1; i++) {
    ll l, r;
    cin >> l >> r;
    if (l > r)
      swap(l, r);
    ans = ans - (l * (n + 1 - r));
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mod 1000000007
#define mo 1000000007
#define mod2 1000000000
#define mod1 998244353
#define lol                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define F(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define ss second

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> v1;
  vector<ll> v2;
  vector<ll> v3;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    if (x >= 0) {
      v1.pb(x);
      v3.pb(x);
    } else if (x < 0) {
      v2.pb(x);
      v3.pb(-x);
    }
  }
  ll k1 = v1.size();
  ll k2 = v2.size();
  ll fl = 0;
  for (ll i = 0; i <= k1; i++) {
    if ((k - i) <= (n - k1) && (k - i) % 2 == 0 && k >= i) {
      fl = 1;
    }
  }
  if (fl == 0) {
    ll pro = 1;
    sort(all(v3));
    for (ll i = 0; i < k; i++) {
      pro = pro * v3[i];
      pro = pro % mo;
    }
    pro = pro * (-1);
    if (pro < 0)
      pro = pro + mo;
    pro = pro % mo;
    cout << pro << endl;
    return 0;
  }
  if (k1 != 0) {
    sort(all(v1));
    reverse(all(v1));
  }
  if (k2 != 0) {
    sort(all(v2));
  }
  ll p1 = 0;
  ll p2 = 0;
  long long pro = 1;
  while (k > 0) {
    if (k == 1) {
      if (p1 < k1) {
        k = k - 1;
        pro = pro * v1[p1];
        pro = pro % mo;
        p1 = p1 + 1;
        continue;
      }
    }
    if (p1 + 1 < k1 && p2 + 1 < k2) {
      if (v1[p1] * v1[p1 + 1] <= v2[p2] * v2[p2 + 1]) {

        k = k - 2;
        pro = pro * v2[p2];
        pro = pro % mo;
        if (pro < 0)
          pro = pro + mo;
        pro = pro * v2[p2 + 1];
        pro = pro % mo;
        if (pro < 0)
          pro = pro + mo;
        p2 = p2 + 2;
        continue;
      } else {
        k = k - 2;
        pro = pro * v1[p1];
        pro = pro % mo;
        pro = pro * v1[p1 + 1];
        pro = pro % mo;
        p1 = p1 + 2;
        continue;
      }
    }
    if (p2 + 1 < k2) {
      k = k - 2;
      pro = pro * v2[p2];
      pro = pro % mo;
      if (pro < 0)
        pro = pro + mo;
      pro = pro * v2[p2 + 1];
      pro = pro % mo;
      if (pro < 0)
        pro = pro + mo;
      p2 = p2 + 2;
      continue;
    }
    if (p1 < k1) {

      k = k - 1;
      pro = pro * v1[p1];
      pro = pro % mo;
      p1 = p1 + 1;
      continue;
    }
  }
  while (pro < 0)
    pro = pro + mo;
  cout << pro << endl;
}