Problem p02815 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;

// const ll MOD = 1000000007;

long long inv(int a, int p) {
  return a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p;
}
static const int MODVAL = 1000000007;
struct mint {
  int val;
  mint() : val(0) {}
  mint(int x) : val(x % MODVAL) {}
  mint(size_t x) : val(x % MODVAL) {}
  mint(long long x) : val(x % MODVAL) {}
  mint &operator+=(mint y) {
    val += y.val;
    if (val >= MODVAL)
      val -= MODVAL;
    return *this;
  }
  mint &operator-=(mint y) {
    val -= y.val;
    if (val < 0)
      val += MODVAL;
    return *this;
  }
  mint &operator*=(mint y) {
    val = (val * (long long)y.val) % MODVAL;
    return *this;
  }
  mint &operator/=(mint y) {
    val = (val * inv(y.val, MODVAL)) % MODVAL;
    return *this;
  }
};
inline mint operator+(mint x, mint y) { return x += y; }
inline mint operator-(mint x, mint y) { return x -= y; }
inline mint operator*(mint x, mint y) { return x *= y; }
inline mint operator/(mint x, mint y) { return x /= y; }
mint POW(mint x, long long n) {
  mint r(1);
  for (; n; x *= x, n >>= 1)
    if (n & 1)
      r *= x;
  return r;
}
mint FAC(int n) {
  static vector<mint> FAC_(1, 1);
  while (int(FAC_.size()) <= n)
    FAC_.push_back(FAC_.back() * FAC_.size());
  return FAC_[n];
}
inline mint CMB(int n, int k) {
  return k < 0 || n < k ? 0 : FAC(n) / (FAC(k) * FAC(n - k));
}
inline ostream &operator<<(ostream &os, mint a) { return os << a.val; }

int n;
ll cs[100000 + 10];

int main(void) {
  scanf("%d", &n);
  REP(i, n) { scanf("%lld", &cs[i]); }
  sort(cs, cs + n);
  reverse(cs, cs + n);
  mint res = 0;
  mint g = 1;
  REP(i, n) {
    // cerr << "> " << g << endl;
    res += mint(cs[i]) * POW(2, n - i - 1) * g;
    g = g * 2 + POW(2, i);
  }
  res *= POW(2, n);
  cout << res << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;

// const ll MOD = 1000000007;

long long inv(int a, int p) {
  return a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p;
}
static const int MODVAL = 1000000007;
struct mint {
  int val;
  mint() : val(0) {}
  mint(int x) : val(x % MODVAL) {}
  mint(size_t x) : val(x % MODVAL) {}
  mint(long long x) : val(x % MODVAL) {}
  mint &operator+=(mint y) {
    val += y.val;
    if (val >= MODVAL)
      val -= MODVAL;
    return *this;
  }
  mint &operator-=(mint y) {
    val -= y.val;
    if (val < 0)
      val += MODVAL;
    return *this;
  }
  mint &operator*=(mint y) {
    val = (val * (long long)y.val) % MODVAL;
    return *this;
  }
  mint &operator/=(mint y) {
    val = (val * inv(y.val, MODVAL)) % MODVAL;
    return *this;
  }
};
inline mint operator+(mint x, mint y) { return x += y; }
inline mint operator-(mint x, mint y) { return x -= y; }
inline mint operator*(mint x, mint y) { return x *= y; }
inline mint operator/(mint x, mint y) { return x /= y; }
mint POW(mint x, long long n) {
  mint r(1);
  for (; n; x *= x, n >>= 1)
    if (n & 1)
      r *= x;
  return r;
}
mint FAC(int n) {
  static vector<mint> FAC_(1, 1);
  while (int(FAC_.size()) <= n)
    FAC_.push_back(FAC_.back() * FAC_.size());
  return FAC_[n];
}
inline mint CMB(int n, int k) {
  return k < 0 || n < k ? 0 : FAC(n) / (FAC(k) * FAC(n - k));
}
inline ostream &operator<<(ostream &os, mint a) { return os << a.val; }

int n;
ll cs[200000 + 10];

int main(void) {
  scanf("%d", &n);
  REP(i, n) { scanf("%lld", &cs[i]); }
  sort(cs, cs + n);
  reverse(cs, cs + n);
  mint res = 0;
  mint g = 1;
  REP(i, n) {
    // cerr << "> " << g << endl;
    res += mint(cs[i]) * POW(2, n - i - 1) * g;
    g = g * 2 + POW(2, i);
  }
  res *= POW(2, n);
  cout << res << endl;
  return 0;
}