Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define nes                                                                    \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);
using namespace std;
const ll m = 1e9 + 7;
ll mod(ll x) { return ((x % m + m) % m); }
ll add(ll a, ll b) { return (mod(mod(a) + mod(b))); }
ll mul(ll a, ll b) { return (mod(mod(a) * mod(b))); }
int main() {
  nes;
  ll n;
  cin >> n;
  vector<ll> v(n);
  ll sum = 0, sum1 = 0, res = 0;
  for (ll i = 0; i < n; ++i) {
    cin >> v[i];
    sum = add(sum, v[i]);
  }
  for (ll i = 0; i < n; ++i) {
    sum -= v[i];
    sum = mod(sum);
    res = add(res, mul(sum, v[i]));
  }
  cout << res;
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<vector<p64>> vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 200003;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 1e18
#define fast_cin()                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define zero ll(0)

ll mpow(ll a, ll b) {
  if (a == 0)
    return 0;
  if (b == 0)
    return 1;
  ll t1 = mpow(a, b / 2);
  t1 *= t1;
  t1 %= MOD;
  if (b % 2)
    t1 *= a;
  t1 %= MOD;
  return t1;
}

ll mpow(ll a, ll b, ll p) {
  if (a == 0)
    return 0;
  if (b == 0)
    return 1;
  ll t1 = mpow(a, b / 2, p);
  t1 *= t1;
  t1 %= p;
  if (b % 2)
    t1 *= a;
  t1 %= p;
  return t1;
}

ll modinverse(ll a, ll m) {
  ll m0 = m;
  ll y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}

mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

ll range(ll l, ll r) { return l + mt() % (r - l + 1); }

ll rev(ll v) { return mpow(v, MOD - 2); }

typedef complex<double> cd;
const double PI = acos(-1);

void fft(vector<cd> &a, bool invert) {
  int n = a.size();

  for (int i = 1, j = 0; i < n; i++) {
    int bit = n >> 1;
    for (; j & bit; bit >>= 1)
      j ^= bit;
    j ^= bit;

    if (i < j)
      swap(a[i], a[j]);
  }

  for (int len = 2; len <= n; len <<= 1) {
    double ang = 2 * PI / len * (invert ? -1 : 1);
    cd wlen(cos(ang), sin(ang));
    for (int i = 0; i < n; i += len) {
      cd w(1);
      for (int j = 0; j < len / 2; j++) {
        cd u = a[i + j], v = a[i + j + len / 2] * w;
        a[i + j] = u + v;
        a[i + j + len / 2] = u - v;
        w *= wlen;
      }
    }
  }

  if (invert) {
    for (cd &x : a)
      x /= n;
  }
}

vector<ll> multiply(vector<ll> const &a, vector<ll> const &b) {
  vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  int n = 1;
  while (n < a.size() + b.size())
    n <<= 1;
  fa.resize(n);
  fb.resize(n);

  fft(fa, false);
  fft(fb, false);
  for (int i = 0; i < n; i++)
    fa[i] *= fb[i];
  fft(fa, true);

  vector<ll> result(n);
  for (int i = 0; i < n; i++)
    result[i] = round(fa[i].real());
  return result;
}

void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  ll g = 2;
  ll m[MOD], r[MOD];
  m[0] = 1;
  r[1] = 0;
  forsn(i, 1, MOD - 1) {
    m[i] = (m[i - 1] * g) % MOD;
    r[m[i]] = i;
  }
  v64 a(MOD - 1, 0);
  forn(i, n) {
    ll t;
    cin >> t;
    ans -= (t * t) % MOD;
    if (t)
      a[r[t]]++;
  }
  a = multiply(a, a);
  forn(i, MOD - 1) { ans += (a[i] + a[i + MOD - 1]) * m[i]; }
  ans /= 2;
  cout << ans << ln;
}

int main() {
  fast_cin();
  ll t = 1;
  // cin >> t;
  forn(i, t) {
    // cout << "Case #" << i+1 << ": ";
    solve();
  }
  return 0;
}