Problem p02815 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

struct mint {
  const int mod = 1e9 + 7;
  long long x;

  mint(long long x = 0) : x((x % mod + mod) % mod) {}

  mint &operator=(const long long a) {
    x = a % mod;
    return *this;
  }

  mint &operator=(const mint a) {
    x = a.x % mod;
    return *this;
  }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint &operator-=(const mint a) {
    if ((x += (mod - a.x)) >= mod)
      x -= mod;
    return *this;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(mod - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<ll> c(n);
  rep(i, n) cin >> c[i];
  sort(all(c));
  vector<mint> e(n + 1);
  rep(i, n + 1) {
    if (i == 0)
      e[i] = 1;
    else
      e[i] = e[i - 1] * 2;
  }
  mint ans = 0;
  rep(i, n) {
    ll l = i, r = n - i - 1;
    ans += e[l] * (e[r] + e[r - 1] * r) * c[i];
  }
  ans *= e[n];
  cout << ans.x << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

struct mint {
  const int mod = 1e9 + 7;
  long long x;

  mint(long long x = 0) : x((x % mod + mod) % mod) {}

  mint &operator=(const long long a) {
    x = a % mod;
    return *this;
  }

  mint &operator=(const mint a) {
    x = a.x % mod;
    return *this;
  }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint &operator-=(const mint a) {
    if ((x += (mod - a.x)) >= mod)
      x -= mod;
    return *this;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(mod - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<ll> c(n);
  rep(i, n) cin >> c[i];
  sort(all(c));
  vector<mint> e(n + 1);
  rep(i, n + 1) {
    if (i == 0)
      e[i] = 1;
    else
      e[i] = e[i - 1] * 2;
  }
  mint ans = 0;
  rep(i, n) {
    ll l = i, r = n - i - 1;
    mint tmp = e[r];
    if (r > 0)
      tmp += e[r - 1] * r;
    tmp *= e[l];
    tmp *= c[i];
    ans += tmp;
  }
  ans *= e[n];
  cout << ans.x << endl;
  return 0;
}