Problem p03764 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1000000007;

// auto mod int
// ref. https://github.com/atcoder-live/library/blob/master/mint.cpp
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  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;
  }

  // for prime MOD
  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;
  }
};

vector<ll> x;
vector<ll> y;

vector<ll> sx;
vector<ll> sy;

int main(void) {
  ll _n, _m;
  cin >> _n >> _m;

  // preparation

  mint n(_n);
  mint m(_m);

  x.resize(_n + 1, 0);
  sx.resize(_n + 1, 0);
  for (int i = 0; i < _n; i++) {
    cin >> x[i + 1];
    sx[i + 1] = sx[i] + x[i + 1];
  }

  y.resize(_m + 1, 0);
  sy.resize(_n + 1, 0);
  for (int i = 0; i < _m; i++) {
    cin >> y[i + 1];
    sy[i + 1] = sy[i] + y[i + 1];
  }

  // calculation

  mint term1(0);
  for (int i = 1; i < _n; i++) {
    term1 += sx[_n] - sx[i];
  }
  mint term2(0);
  for (int i = 1; i < _n; i++) {
    term2 += x[i] * ll(_n - (i + 1) + 1);
  }
  mint term3(0);
  for (int i = 1; i < _m; i++) {
    term3 += sy[_m] - sy[i];
  }
  mint term4(0);
  for (int i = 1; i < _m; i++) {
    term4 += y[i] * ll(_m - (i + 1) + 1);
  }
  // printf("temr1: %lld, term2: %lld, term3: %lld, term4: %lld\n", term1.x,
  // term2.x, term3.x, term4.x);
  mint ans = term1 * term3 - term1 * term4 - term2 * term3 + term2 * term4;

  cout << ans.x << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1000000007;

// auto mod int
// ref. https://github.com/atcoder-live/library/blob/master/mint.cpp
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  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;
  }

  // for prime MOD
  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;
  }
};

vector<ll> x;
vector<ll> y;

vector<ll> sx;
vector<ll> sy;

int main(void) {
  ll _n, _m;
  cin >> _n >> _m;

  // preparation

  mint n(_n);
  mint m(_m);

  x.resize(_n + 1, 0);
  sx.resize(_n + 1, 0);
  for (int i = 0; i < _n; i++) {
    cin >> x[i + 1];
    sx[i + 1] = sx[i] + x[i + 1];
  }

  y.resize(_m + 1, 0);
  sy.resize(_m + 1, 0);
  for (int i = 0; i < _m; i++) {
    cin >> y[i + 1];
    sy[i + 1] = sy[i] + y[i + 1];
  }

  // calculation

  mint term1(0);
  for (int i = 1; i < _n; i++) {
    term1 += sx[_n] - sx[i];
  }
  mint term2(0);
  for (int i = 1; i < _n; i++) {
    term2 += x[i] * ll(_n - (i + 1) + 1);
  }
  mint term3(0);
  for (int i = 1; i < _m; i++) {
    term3 += sy[_m] - sy[i];
  }
  mint term4(0);
  for (int i = 1; i < _m; i++) {
    term4 += y[i] * ll(_m - (i + 1) + 1);
  }
  // printf("temr1: %lld, term2: %lld, term3: %lld, term4: %lld\n", term1.x,
  // term2.x, term3.x, term4.x);
  mint ans = term1 * term3 - term1 * term4 - term2 * term3 + term2 * term4;

  cout << ans.x << endl;

  return 0;
}