Problem p02815 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e2 + 7;
const int MOD = 1e9 + 7;
typedef pair<int, int> pii;

template <int _MOD> struct Mint {
  int v = 0;
  Mint() {}
  Mint(int _v) : v((_v % _MOD + _MOD) % _MOD) {}
  Mint(long long _v) : v(static_cast<int>((_v % _MOD + _MOD) % _MOD)) {}
  Mint operator=(const int &_v) { return *this = Mint(_v); }
  Mint operator=(const long long &_v) { return *this = Mint(_v); }
  bool operator!() const { return !this->v; }
  bool operator<(const Mint &b) const { return v < b.v; }
  bool operator>(const Mint &b) const { return v > b.v; }
  bool operator==(const Mint &b) const { return v == b.v; }
  bool operator!=(const Mint &b) const { return v != b.v; }
  bool operator<=(const Mint &b) const { return v < b.v || v == b.v; }
  bool operator>=(const Mint &b) const { return v > b.v || v == b.v; }
  Mint operator+(const Mint &b) const { return Mint(v + b.v); }
  Mint operator-(const Mint &b) const { return Mint(v - b.v); }
  Mint operator*(const Mint &b) const { return Mint(1ll * v * b.v); }
  Mint operator/(const Mint &b) const { return Mint(b.inv() * v); }
  Mint &operator+=(const Mint &b) { return *this = *this + b; }
  Mint &operator-=(const Mint &b) { return *this = *this - b; }
  Mint &operator*=(const Mint &b) { return *this = *this * b; }
  Mint &operator/=(const Mint &b) { return *this = *this / b; }
  Mint operator-() const { return Mint(-v); }
  Mint &operator++() { return *this += 1; }
  Mint &operator--() { return *this -= 1; }
  Mint operator++(int) {
    Mint tmp = *this;
    *this += 1;
    return tmp;
  }
  Mint operator--(int) {
    Mint tmp = *this;
    *this -= 1;
    return tmp;
  }
  Mint pow(int p) const {
    Mint res(1), x(*this);
    while (p) {
      if (p & 1)
        res = res * x;
      x *= x;
      p >>= 1;
    }
    return res;
  }
  Mint inv() const { return pow(_MOD - 2); }
  friend istream &operator>>(istream &is, Mint &mt) { return is >> mt.v; }
  friend ostream &operator<<(ostream &os, const Mint &mt) { return os << mt.v; }
};
using mint = Mint<MOD>;

int n;
int a[N];

inline void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  sort(a + 1, a + n + 1);
  reverse(a + 1, a + n + 1);
  mint res = 0, pow4 = 1;
  for (int i = 1; i < n; ++i)
    pow4 *= 4;
  for (int i = 1; i <= n; ++i)
    res += mint(i + 1) * a[i];
  res *= pow4;
  cout << res << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;
const int MOD = 1e9 + 7;
typedef pair<int, int> pii;

template <int _MOD> struct Mint {
  int v = 0;
  Mint() {}
  Mint(int _v) : v((_v % _MOD + _MOD) % _MOD) {}
  Mint(long long _v) : v(static_cast<int>((_v % _MOD + _MOD) % _MOD)) {}
  Mint operator=(const int &_v) { return *this = Mint(_v); }
  Mint operator=(const long long &_v) { return *this = Mint(_v); }
  bool operator!() const { return !this->v; }
  bool operator<(const Mint &b) const { return v < b.v; }
  bool operator>(const Mint &b) const { return v > b.v; }
  bool operator==(const Mint &b) const { return v == b.v; }
  bool operator!=(const Mint &b) const { return v != b.v; }
  bool operator<=(const Mint &b) const { return v < b.v || v == b.v; }
  bool operator>=(const Mint &b) const { return v > b.v || v == b.v; }
  Mint operator+(const Mint &b) const { return Mint(v + b.v); }
  Mint operator-(const Mint &b) const { return Mint(v - b.v); }
  Mint operator*(const Mint &b) const { return Mint(1ll * v * b.v); }
  Mint operator/(const Mint &b) const { return Mint(b.inv() * v); }
  Mint &operator+=(const Mint &b) { return *this = *this + b; }
  Mint &operator-=(const Mint &b) { return *this = *this - b; }
  Mint &operator*=(const Mint &b) { return *this = *this * b; }
  Mint &operator/=(const Mint &b) { return *this = *this / b; }
  Mint operator-() const { return Mint(-v); }
  Mint &operator++() { return *this += 1; }
  Mint &operator--() { return *this -= 1; }
  Mint operator++(int) {
    Mint tmp = *this;
    *this += 1;
    return tmp;
  }
  Mint operator--(int) {
    Mint tmp = *this;
    *this -= 1;
    return tmp;
  }
  Mint pow(int p) const {
    Mint res(1), x(*this);
    while (p) {
      if (p & 1)
        res = res * x;
      x *= x;
      p >>= 1;
    }
    return res;
  }
  Mint inv() const { return pow(_MOD - 2); }
  friend istream &operator>>(istream &is, Mint &mt) { return is >> mt.v; }
  friend ostream &operator<<(ostream &os, const Mint &mt) { return os << mt.v; }
};
using mint = Mint<MOD>;

int n;
int a[N];

inline void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  sort(a + 1, a + n + 1);
  reverse(a + 1, a + n + 1);
  mint res = 0, pow4 = 1;
  for (int i = 1; i < n; ++i)
    pow4 *= 4;
  for (int i = 1; i <= n; ++i)
    res += mint(i + 1) * a[i];
  res *= pow4;
  cout << res << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}