Problem p03762 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define all(a) a.begin(), a.end()
#define VV(T) vector<vector<T>>
#define rep(i, N) for (int i = 0; i < N; i++)
#define repsg(i, s, g) for (int i = s; i < g; i++)
#define name(x) #x
using namespace std;

const int INF = 10000000;
const long long INFL = (1LL << 50);
const double PI = atan(1) * 4;
const int MOD = pow(10, 9) + 7;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

typedef long long ll, LL;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;

// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> x(n);
  vector<ll> y(m);
  cin >> x >> y;
  sort(all(x));
  sort(all(y));
  ll x_direc = 0;
  ll y_direc = 0;
  rep(i, n) { x_direc += (i)*x[i] - (n - 1 - i) * x[i]; }
  rep(i, n) { y_direc += (i)*y[i] - (m - 1 - i) * y[i]; }
  x_direc %= MOD;
  y_direc %= MOD;

  ll out = x_direc * y_direc;
  out %= MOD;
  cout << out << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define all(a) a.begin(), a.end()
#define VV(T) vector<vector<T>>
#define rep(i, N) for (int i = 0; i < N; i++)
#define repsg(i, s, g) for (int i = s; i < g; i++)
#define name(x) #x
using namespace std;

const int INF = 10000000;
const long long INFL = (1LL << 50);
const double PI = atan(1) * 4;
const int MOD = pow(10, 9) + 7;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

typedef long long ll, LL;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;

// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> x(n);
  vector<ll> y(m);
  cin >> x >> y;
  sort(all(x));
  sort(all(y));
  ll x_direc = 0;
  ll y_direc = 0;
  rep(i, n) {
    x_direc += (i)*x[i] - (n - 1 - i) * x[i];
    x_direc %= MOD;
  }
  rep(i, m) {
    y_direc += (i)*y[i] - (m - 1 - i) * y[i];
    y_direc %= MOD;
  }
  x_direc = (x_direc + MOD) % MOD;
  y_direc = (y_direc + MOD) % MOD;

  ll out = x_direc * y_direc;
  out %= MOD;
  cout << out << endl;

  return 0;
}