Problem p03764 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define POW(n) ((n) * (n))
#define ALL(a) (a).begin(), (a).end()
#define dump(v) (cerr << #v << ": " << v << endl)
#define cerr                                                                   \
  if (true)                                                                    \
  cerr

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<unsigned long long> vull;

// ll n = to_T<ll>("114514")
template <class T> T to_T(const string &s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}
template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}

template <long long modulo> class Modulo {
private:
  long long inv() {
    long long p = mod - 2;
    long long ans = 1;
    for (; p != 0; p >>= 1LL) {
      if (p & 1LL)
        ans = (ans * x) % mod;
      x = (x * x) % mod;
    }
    return ans;
  }

public:
  long long mod = modulo;
  unsigned long long x;
  Modulo() : x(0) {}
  Modulo(signed int s) {
    while (s < 0)
      s += mod;
    x = s % mod;
  }
  Modulo(signed long long s) {
    while (s < 0)
      s += mod;
    x = s % mod;
  }
  operator int() { return (int)x; }
  operator long long() { return (long long)x; }
  Modulo &operator+=(Modulo q) {
    if ((x += q.x) >= mod)
      x -= mod;
    return *this;
  }
  Modulo &operator-=(Modulo q) {
    if ((x += mod - q.x) >= mod)
      x -= mod;
    return *this;
  }
  Modulo &operator*=(Modulo q) {
    x = (unsigned long long)x * q.x % mod;
    return *this;
  }
  Modulo &operator/=(Modulo q) {
    x = (unsigned long long)x * q.inv() % mod;
    return *this;
  }
  Modulo operator+(Modulo q) { return Modulo(*this) += q; }
  Modulo operator-(Modulo q) { return Modulo(*this) -= q; }
  Modulo operator*(Modulo q) { return Modulo(*this) *= q; }
  Modulo operator/(Modulo q) { return Modulo(*this) /= q; }
  template <typename T> Modulo &operator+=(const T q) {
    if ((x += q) >= mod)
      x -= mod;
    return *this;
  }
  template <typename T> Modulo &operator-=(const T q) {
    if ((x += mod - q) >= mod)
      x -= mod;
    return *this;
  }
  template <typename T> Modulo &operator*=(const T q) {
    x = (unsigned long long)x * q % mod;
    return *this;
  }
  template <typename T> Modulo &operator/=(const T q) {
    x = (unsigned long long)x * (Modulo(q)).inv() % mod;
    return *this;
  }
  template <typename T> Modulo operator+(const T q) {
    return Modulo(*this) += q;
  }
  template <typename T> Modulo operator-(const T q) {
    return Modulo(*this) -= q;
  }
  template <typename T> Modulo operator*(const T q) {
    return Modulo(*this) *= q;
  }
  template <typename T> Modulo operator/(const T q) {
    return Modulo(*this) /= q;
  }
};

const ll MOD = 1000000007LL;

void solve(long long n, long long m, vector<long long> x, vector<long long> y) {
  Modulo<1000000007> xx(0), yy(0);
  const ll MOD = 1000000007L;
  REP(x1, n - 1) for (int x2 = x1 + 1; x2 < n; ++x2) { xx += x[x2] - x[x1]; }
  REP(y1, m - 1) for (int y2 = y1 + 1; y2 < m; ++y2) { yy += y[y2] - y[y1]; }

  cout << (ll)(xx * yy) << endl;
}

int main() {
  ios::sync_with_stdio(false);
  long long n;
  long long m;
  cin >> n;
  vector<long long> x(n - 1 + 1);
  cin >> m;
  vector<long long> y(m - 1 + 1);
  for (int i = 0; i <= n - 1; i++) {
    cin >> x[i];
  }
  for (int i = 0; i <= m - 1; i++) {
    cin >> y[i];
  }
  solve(n, m, x, y);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define POW(n) ((n) * (n))
#define ALL(a) (a).begin(), (a).end()
#define dump(v) (cerr << #v << ": " << v << endl)
#define cerr                                                                   \
  if (true)                                                                    \
  cerr

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<unsigned long long> vull;

// ll n = to_T<ll>("114514")
template <class T> T to_T(const string &s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}
template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}

template <long long modulo> class Modulo {
private:
  long long inv() {
    long long p = mod - 2;
    long long ans = 1;
    for (; p != 0; p >>= 1LL) {
      if (p & 1LL)
        ans = (ans * x) % mod;
      x = (x * x) % mod;
    }
    return ans;
  }

public:
  long long mod = modulo;
  unsigned long long x;
  Modulo() : x(0) {}
  Modulo(signed int s) {
    while (s < 0)
      s += mod;
    x = s % mod;
  }
  Modulo(signed long long s) {
    while (s < 0)
      s += mod;
    x = s % mod;
  }
  operator int() { return (int)x; }
  operator long long() { return (long long)x; }
  Modulo &operator+=(Modulo q) {
    if ((x += q.x) >= mod)
      x -= mod;
    return *this;
  }
  Modulo &operator-=(Modulo q) {
    if ((x += mod - q.x) >= mod)
      x -= mod;
    return *this;
  }
  Modulo &operator*=(Modulo q) {
    x = (unsigned long long)x * q.x % mod;
    return *this;
  }
  Modulo &operator/=(Modulo q) {
    x = (unsigned long long)x * q.inv() % mod;
    return *this;
  }
  Modulo operator+(Modulo q) { return Modulo(*this) += q; }
  Modulo operator-(Modulo q) { return Modulo(*this) -= q; }
  Modulo operator*(Modulo q) { return Modulo(*this) *= q; }
  Modulo operator/(Modulo q) { return Modulo(*this) /= q; }
  template <typename T> Modulo &operator+=(const T q) {
    if ((x += q) >= mod)
      x -= mod;
    return *this;
  }
  template <typename T> Modulo &operator-=(const T q) {
    if ((x += mod - q) >= mod)
      x -= mod;
    return *this;
  }
  template <typename T> Modulo &operator*=(const T q) {
    x = (unsigned long long)x * q % mod;
    return *this;
  }
  template <typename T> Modulo &operator/=(const T q) {
    x = (unsigned long long)x * (Modulo(q)).inv() % mod;
    return *this;
  }
  template <typename T> Modulo operator+(const T q) {
    return Modulo(*this) += q;
  }
  template <typename T> Modulo operator-(const T q) {
    return Modulo(*this) -= q;
  }
  template <typename T> Modulo operator*(const T q) {
    return Modulo(*this) *= q;
  }
  template <typename T> Modulo operator/(const T q) {
    return Modulo(*this) /= q;
  }
};

const ll MOD = 1000000007LL;

void solve(long long n, long long m, vector<long long> x, vector<long long> y) {
  Modulo<1000000007> xx(0), yy(0);
  const ll MOD = 1000000007L;
  FOR(i, 1, n + 1) {
    xx += (Modulo<1000000007>)(i - 1) * (Modulo<1000000007>)x[i - 1];
    xx -= (Modulo<1000000007>)(n - i) * (Modulo<1000000007>)x[i - 1];
  }
  FOR(i, 1, m + 1) {
    yy += (Modulo<1000000007>)(i - 1) * (Modulo<1000000007>)y[i - 1];
    yy -= (Modulo<1000000007>)(m - i) * (Modulo<1000000007>)y[i - 1];
  }

  cout << (ll)(xx * yy) << endl;
}

int main() {
  ios::sync_with_stdio(false);
  long long n;
  long long m;
  cin >> n;
  vector<long long> x(n - 1 + 1);
  cin >> m;
  vector<long long> y(m - 1 + 1);
  for (int i = 0; i <= n - 1; i++) {
    cin >> x[i];
  }
  for (int i = 0; i <= m - 1; i++) {
    cin >> y[i];
  }
  solve(n, m, x, y);
  return 0;
}