Problem p03762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define INF bit(60)
#define pb push_back
#define mod 1000000007

using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
using tTree = __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>,
                               __gnu_pbds::rb_tree_tag,
                               __gnu_pbds::tree_order_statistics_node_update>;

ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

#define FACSIZE 200002

ll invfac[FACSIZE];
ll fac[FACSIZE];

template <class T> bool chmax(T &, const T &);
template <class T> bool chmin(T &, const T &);
ll gcd(ll, ll);
ll powLL(ll x, ll y);

ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
ll comb(ll, ll);
void make_fact(ll);
void make_invfact(ll);
void fact_init();

int main(void) {
  ll n, m;
  cin >> n >> m;
  vector<ll> X(n);
  vector<ll> Y(m);

  rep(i, n) cin >> X[i];
  rep(i, m) cin >> Y[i];

  vector<ll> dX(n - 1);
  vector<ll> dY(m - 1);
  dX[0] = n - 1;
  dX[n - 2] = n - 1;

  for (ll i = 1; i < n / 2; i++) {
    dX[i] = dX[i - 1] + (n - 1 - 2 * i);
    dX[n - 2 - i] = dX[i];
  }
  dY[0] = m - 1;
  dY[m - 2] = m - 1;
  for (ll i = 1; i < m / 2; i++) {
    dY[i] = dY[i - 1] + (m - 1 - 2 * i);
    dY[m - 2 - i] = dY[i];
  }

  ll xs = 0;
  for (ll i = 0; i < n - 1; i++) {
    xs = mod_add(xs, mod_mul((X[i + 1] - X[i] + mod), dX[i]));
  }

  ll ans = 0;
  for (ll i = 0; i < n - 1; i++) {
    ll y = mod_mul((Y[i + 1] - Y[i] + mod), dY[i]);
    ans = mod_add(ans, mod_mul(y, xs));
  }
  cout << ans << endl;

  /*
  for (auto x : dX)
  {
    cout << x << " ";
  }
  cout << endl;
  for (auto y : dY)
  {
    cout << y << " ";
  }
  cout << endl;
  //*/

  return 0;
}

ll mod_pow(ll x, ll r) {
  if (r == 0)
    return 1;
  else if (r == 1)
    return x % mod;
  else if (r % 2 == 0) {
    ll t = mod_pow(x, r / 2) % mod;
    return mod_mul(t, t);
  } else {
    ll t = mod_pow(x, r / 2) % mod;
    ll k = mod_mul(t, t);
    return (k % mod) * (x % mod);
  }
}
ll mod_add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }

ll mod_mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }

ll mod_div(ll a, ll b) { return mod_mul(a, mod_pow(b, mod - 2)); }

void fact_init() {
  make_fact(FACSIZE - 1);
  make_invfact(FACSIZE);
}

void make_fact(ll n) {
  fac[0] = 1;
  rep(i, n) { fac[i + 1] = mod_mul(fac[i], i + 1); }
}

void make_invfact(ll n) {
  invfac[n] = mod_pow(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--) {
    invfac[i] = mod_mul(invfac[i + 1], i + 1);
  }
}

ll comb(ll n, ll r) {

  return mod_mul(mod_mul(fac[n], invfac[r]), invfac[n - r]);
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll qp(ll a, ll b) {
  ll ans = 1LL;
  do {
    if (b & 1LL)
      ans = 1LL * mod_mul(ans, a) % mod;
    a = 1LL * mod_mul(a, a) % mod;
  } while (b >>= 1LL);
  return ans;
}
ll qp(ll a, ll b, ll mo) {
  ll ans = 1LL;
  do {
    if (b & 1LL)
      ans = 1LL * (ans % mo) * (a % mo);
    a = 1LL * (a % mo) * (a % mo);
  } while (b >>= 1LL);
  return ans;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll powLL(ll x, ll y) {
  ll ans = 1LL;
  for (ll i = 0LL; i < y; i++)
    ans *= x;
  return ans;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define INF bit(60)
#define pb push_back
#define mod 1000000007

using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
using tTree = __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>,
                               __gnu_pbds::rb_tree_tag,
                               __gnu_pbds::tree_order_statistics_node_update>;

ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

#define FACSIZE 200002

ll invfac[FACSIZE];
ll fac[FACSIZE];

template <class T> bool chmax(T &, const T &);
template <class T> bool chmin(T &, const T &);
ll gcd(ll, ll);
ll powLL(ll x, ll y);

ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
ll comb(ll, ll);
void make_fact(ll);
void make_invfact(ll);
void fact_init();

int main(void) {
  ll n, m;
  cin >> n >> m;
  vector<ll> X(n);
  vector<ll> Y(m);

  rep(i, n) cin >> X[i];
  rep(i, m) cin >> Y[i];

  vector<ll> dX(n - 1);
  vector<ll> dY(m - 1);
  dX[0] = n - 1;
  dX[n - 2] = n - 1;

  for (ll i = 1; i < n / 2; i++) {
    dX[i] = dX[i - 1] + (n - 1 - 2 * i);
    dX[n - 2 - i] = dX[i];
  }
  dY[0] = m - 1;
  dY[m - 2] = m - 1;
  for (ll i = 1; i < m / 2; i++) {
    dY[i] = dY[i - 1] + (m - 1 - 2 * i);
    dY[m - 2 - i] = dY[i];
  }

  ll xs = 0;
  for (ll i = 0; i < n - 1; i++) {
    xs = mod_add(xs, mod_mul((X[i + 1] - X[i] + mod), dX[i]));
  }

  ll ans = 0;
  for (ll i = 0; i < m - 1; i++) {
    ll y = mod_mul((Y[i + 1] - Y[i] + mod), dY[i]);
    ans = mod_add(ans, mod_mul(y, xs));
  }
  cout << ans << endl;

  /*
  for (auto x : dX)
  {
    cout << x << " ";
  }
  cout << endl;
  for (auto y : dY)
  {
    cout << y << " ";
  }
  cout << endl;
  //*/

  return 0;
}

ll mod_pow(ll x, ll r) {
  if (r == 0)
    return 1;
  else if (r == 1)
    return x % mod;
  else if (r % 2 == 0) {
    ll t = mod_pow(x, r / 2) % mod;
    return mod_mul(t, t);
  } else {
    ll t = mod_pow(x, r / 2) % mod;
    ll k = mod_mul(t, t);
    return (k % mod) * (x % mod);
  }
}
ll mod_add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }

ll mod_mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }

ll mod_div(ll a, ll b) { return mod_mul(a, mod_pow(b, mod - 2)); }

void fact_init() {
  make_fact(FACSIZE - 1);
  make_invfact(FACSIZE);
}

void make_fact(ll n) {
  fac[0] = 1;
  rep(i, n) { fac[i + 1] = mod_mul(fac[i], i + 1); }
}

void make_invfact(ll n) {
  invfac[n] = mod_pow(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--) {
    invfac[i] = mod_mul(invfac[i + 1], i + 1);
  }
}

ll comb(ll n, ll r) {

  return mod_mul(mod_mul(fac[n], invfac[r]), invfac[n - r]);
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll qp(ll a, ll b) {
  ll ans = 1LL;
  do {
    if (b & 1LL)
      ans = 1LL * mod_mul(ans, a) % mod;
    a = 1LL * mod_mul(a, a) % mod;
  } while (b >>= 1LL);
  return ans;
}
ll qp(ll a, ll b, ll mo) {
  ll ans = 1LL;
  do {
    if (b & 1LL)
      ans = 1LL * (ans % mo) * (a % mo);
    a = 1LL * (a % mo) * (a % mo);
  } while (b >>= 1LL);
  return ans;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll powLL(ll x, ll y) {
  ll ans = 1LL;
  for (ll i = 0LL; i < y; i++)
    ans *= x;
  return ans;
}