Problem p03762 (C++)

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

#define ld long double
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define spa << " " <<
#define MP make_pair
/*#define pmod(x,y) x=(x+y)%MOD;
#define qmod(x,y) x=(x*y)%MOD;*/
const ll MOD = 1e9 + 7;
// ll MOD = 998244353;
ll INF = 1e18;
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
/*void pmod(ll &a,ll b){a=(a+b)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}*/
void ans1(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void ans2(bool x) {
  if (x)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void ans3(bool x) {
  if (x)
    cout << "Yay!" << endl;
  else
    cout << ":(" << endl;
}
void ans(bool x, ll y, ll z) {
  if (x)
    cout << y << endl;
  else
    cout << z << endl;
}
void anss(bool x, string y, string z) {
  if (x)
    cout << y << endl;
  else
    cout << z << endl;
}
ll gcd(ll x, ll y) {
  ll r;
  while ((r = x % y) != 0) {
    x = y;
    y = r;
  }
  return y;
}

int main() {
  ll i, j, o;
  ll res = 0, res1 = 0, res2 = INF, buf = 0, buf1 = 0, buf2 = 0, buf3 = 0,
     buf4 = 0, sum = 0;
  ll l, r;
  bool judge = true;
  ll n, m;
  cin >> n >> m;
  vector<ll> x(n), y(n);
  for (i = 0; i < n; i++)
    cin >> x[i];
  for (i = 0; i < m; i++)
    cin >> y[i];
  ll z1, z2;
  buf = 0;
  for (i = 0; i < n / 2; i++) {
    buf = (buf + n - 2 * i - 1) % MOD;
    l = (x[i + 1] - x[i]) % MOD;
    r = (x[n - i - 1] - x[n - i - 2]) % MOD;
    z1 = (buf * l) % MOD;
    res1 = (z1 + res1) % MOD;
    z2 = (buf * r) % MOD;
    if (i != n - i - 2)
      res1 = (z2 + res1) % MOD;
  }
  buf = 0;
  for (i = 0; i < m / 2; i++) {
    buf = (m - 2 * i - 1 + buf) % MOD;
    l = (y[i + 1] - y[i]) % MOD;
    r = (y[m - i - 1] - y[m - i - 2]) % MOD;
    buf2 = (buf * l) % MOD;
    buf2 = (buf2 * res1) % MOD;
    res = (buf2 + res) % MOD;
    buf2 = (buf * r) % MOD;
    buf2 = (buf2 * res1) % MOD;
    if (i != m - i - 2)
      res = (res + buf2) % MOD;
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ld long double
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define spa << " " <<
#define MP make_pair
/*#define pmod(x,y) x=(x+y)%MOD;
#define qmod(x,y) x=(x*y)%MOD;*/
const ll MOD = 1e9 + 7;
// ll MOD = 998244353;
ll INF = 1e18;
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
/*void pmod(ll &a,ll b){a=(a+b)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}*/
void ans1(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void ans2(bool x) {
  if (x)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void ans3(bool x) {
  if (x)
    cout << "Yay!" << endl;
  else
    cout << ":(" << endl;
}
void ans(bool x, ll y, ll z) {
  if (x)
    cout << y << endl;
  else
    cout << z << endl;
}
void anss(bool x, string y, string z) {
  if (x)
    cout << y << endl;
  else
    cout << z << endl;
}
ll gcd(ll x, ll y) {
  ll r;
  while ((r = x % y) != 0) {
    x = y;
    y = r;
  }
  return y;
}

int main() {
  ll i, j, o;
  ll res = 0, res1 = 0, res2 = INF, buf = 0, buf1 = 0, buf2 = 0, buf3 = 0,
     buf4 = 0, sum = 0;
  ll l, r;
  bool judge = true;
  ll n, m;
  cin >> n >> m;
  vector<ll> x(n), y(m);
  for (i = 0; i < n; i++)
    cin >> x[i];
  for (i = 0; i < m; i++)
    cin >> y[i];
  ll z1, z2;
  buf = 0;
  for (i = 0; i < n / 2; i++) {
    buf = (buf + n - 2 * i - 1) % MOD;
    l = (x[i + 1] - x[i]) % MOD;
    r = (x[n - i - 1] - x[n - i - 2]) % MOD;
    z1 = (buf * l) % MOD;
    res1 = (z1 + res1) % MOD;
    z2 = (buf * r) % MOD;
    if (i != n - i - 2)
      res1 = (z2 + res1) % MOD;
  }
  buf = 0;
  for (i = 0; i < m / 2; i++) {
    buf = (m - 2 * i - 1 + buf) % MOD;
    l = (y[i + 1] - y[i]) % MOD;
    r = (y[m - i - 1] - y[m - i - 2]) % MOD;
    buf2 = (buf * l) % MOD;
    buf2 = (buf2 * res1) % MOD;
    res = (buf2 + res) % MOD;
    buf2 = (buf * r) % MOD;
    buf2 = (buf2 * res1) % MOD;
    if (i != m - i - 2)
      res = (res + buf2) % MOD;
  }
  cout << res << endl;
  return 0;
}