Problem p03762 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = int64_t;
using ull = uint64_t;

template <typename T> vector<T> make_vec_nd(T init, size_t size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}

struct Mod {
  static ll BASE;
  ll value = 0;
  static ll residue(ll value) { return (value % BASE + BASE) % BASE; }
  Mod(ll value) : value(residue(value)) {}
  Mod operator+(const Mod &rhs) const { return Mod(value + rhs.value); }
  void operator+=(const Mod &rhs) { *this = *this + rhs; }
  Mod operator-(const Mod &rhs) const { return Mod(value - rhs.value); }
  void operator-=(const Mod &rhs) { *this = *this - rhs; }
  Mod operator*(const Mod &rhs) const { return Mod(value * rhs.value); }
  void operator*=(const Mod &rhs) { *this = *this * rhs; }
};
ll Mod::BASE = 1000000007;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> xs(N);
  for (ll i = 0; i < N; i++) {
    cin >> xs[i];
  }
  vector<ll> ys(N);
  for (ll i = 0; i < M; i++) {
    cin >> ys[i];
  }
  Mod ans_x = 0;
  for (ll i = 0; i < N - 1; i++) {
    ans_x +=
        ((Mod)xs[i + 1] - (Mod)xs[i]) * ((Mod)N - 1 - (Mod)i) * ((Mod)i + 1);
  }
  Mod ans_y = 0;
  for (ll i = 0; i < M - 1; i++) {
    ans_y +=
        ((Mod)ys[i + 1] - (Mod)ys[i]) * ((Mod)M - 1 - (Mod)i) * ((Mod)i + 1);
  }
  Mod ans = ans_x * ans_y;
  cout << ans.value << endl;
}


Fixed submission:
//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = int64_t;
using ull = uint64_t;

template <typename T> vector<T> make_vec_nd(T init, size_t size) {
  return vector<T>(size, init);
}
template <typename T, typename... Args>
auto make_vec_nd(T init, size_t size, Args... rest) {
  auto inner = make_vec_nd(init, rest...);
  return vector<decltype(inner)>(size, inner);
}

struct Mod {
  static ll BASE;
  ll value = 0;
  static ll residue(ll value) { return (value % BASE + BASE) % BASE; }
  Mod(ll value) : value(residue(value)) {}
  Mod operator+(const Mod &rhs) const { return Mod(value + rhs.value); }
  void operator+=(const Mod &rhs) { *this = *this + rhs; }
  Mod operator-(const Mod &rhs) const { return Mod(value - rhs.value); }
  void operator-=(const Mod &rhs) { *this = *this - rhs; }
  Mod operator*(const Mod &rhs) const { return Mod(value * rhs.value); }
  void operator*=(const Mod &rhs) { *this = *this * rhs; }
};
ll Mod::BASE = 1000000007;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> xs(N);
  for (ll i = 0; i < N; i++) {
    cin >> xs[i];
  }
  vector<ll> ys(M);
  for (ll i = 0; i < M; i++) {
    cin >> ys[i];
  }
  Mod ans_x = 0;
  for (ll i = 0; i < N - 1; i++) {
    ans_x +=
        ((Mod)xs[i + 1] - (Mod)xs[i]) * ((Mod)N - 1 - (Mod)i) * ((Mod)i + 1);
  }
  Mod ans_y = 0;
  for (ll i = 0; i < M - 1; i++) {
    ans_y +=
        ((Mod)ys[i + 1] - (Mod)ys[i]) * ((Mod)M - 1 - (Mod)i) * ((Mod)i + 1);
  }
  Mod ans = ans_x * ans_y;
  cout << ans.value << endl;
}