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

#include <bits/stdc++.h>
typedef long long ll;
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
typedef std::vector<long long> vi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mod 1000000007
#define yesno(f)                                                               \\
  yes(f);                                                                      \\
  no(f);
#define yes(f)                                                                 \\
  if (f)                                                                       \\
    cout << "YES\\n";
#define no(f)                                                                  \\
  if (!f)                                                                      \\
    cout << "NO\\n";
using namespace std;
vi adj[200005];
ll mark[200005] = {0}, y;
void dfs(ll x) {
  mark[x] = 1;
  y++;
  fr(i, 0, adj[x].size()) {
    if (mark[adj[x][i]] == 0) {
      dfs(adj[x][i]);
    }
  }
}
int main() {
  fast;
  ll p = 1;
  // std::cin >> t;
  while (p--) {
    ll n;
    std::cin >> n;
    ll presum[n] = {0}, a[n];
    fr(i, 0, n) std::cin >> a[i];
    rf(i, n - 1, 0) {
      if (i == n - 1)
        presum[i] = a[i];
      else
        presum[i] = (presum[i + 1] + a[i]) % mod;
    }
    ll sum = 0;
    fr(i, 0, n - 1) { sum = (sum + (presum[i + 1] * a[i]) % mod) % mod; }
    cout << sum << "\\n";
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream &operator<<(ostream &out, const vector<A> &v) {
  out << "[";
  forn(i, sz(v)) {
    if (i)
      out << ", ";
    out << v[i];
  }
  return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = 200003;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int LOGN = 20;
const int N = (1 << LOGN) + 13;

int n;
int a[N];

bool read() {
  if (scanf("%d", &n) != 1)
    return false;
  forn(i, n) scanf("%d", &a[i]);
  return true;
}

int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  if (a < 0)
    a += MOD;
  return a;
}

int mul(int a, int b) { return a * li(b) % MOD; }

int binpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

struct comp {
  ld x, y;
  comp(){};
  comp(ld x, ld y) : x(x), y(y){};
  comp(ld x) : x(x), y(0){};
  comp conj() { return comp(x, -y); }
  comp operator+(const comp &b) { return comp(x + b.x, y + b.y); }
  comp operator-(const comp &b) { return comp(x - b.x, y - b.y); }
  comp operator*(ld b) { return comp(x * b, y * b); }
  comp operator*(const comp &b) {
    return comp(x * b.x - y * b.y, x * b.y + y * b.x);
  }
  comp operator/(ld b) { return comp(x / b, y / b); }
  comp operator/(const comp &b) {
    return comp((x * b.x + y * b.y) / (b.x * b.x + b.y * b.y),
                (y * b.x - x * b.y) / (b.x * b.x + b.y * b.y));
  }
};

vector<comp> w[LOGN];
vector<int> rv[LOGN];

void prepare() {
  forn(st, LOGN - 1) {
    w[st].resize(1 << st);
    forn(i, 1 << st) {
      ld ang = PI / (1 << st) * i;
      w[st][i] = comp(cosl(ang), sinl(ang));
    }
  }
  forn(st, LOGN) {
    rv[st].resize(1 << st);
    if (st > 0) {
      int h = (1 << (st - 1)) - 1;
      forn(i, 1 << st) rv[st][i] = (rv[st - 1][i & h] << 1) | (i > h);
    }
  }
}

void fft(vector<comp> &a, bool inv) {
  int n = sz(a);
  int ln = __builtin_ctz(n);
  forn(i, n) {
    int ni = rv[ln][i];
    if (i < ni)
      swap(a[i], a[ni]);
  }
  forn(st, ln) {
    int len = 1 << st;
    for (int k = 0; k < n; k += (len << 1)) {
      fore(pos, k, k + len) {
        comp l = a[pos];
        comp r = a[pos + len] * w[st][pos - k];
        a[pos] = l + r;
        a[pos + len] = l - r;
      }
    }
  }
  if (inv) {
    forn(i, n) a[i] = a[i] / n;
    reverse(a.begin() + 1, a.end());
  }
}

vector<li> mul(vector<int> a, vector<int> b) {
  int cnt = 1 << (32 - __builtin_clz(sz(a) + sz(b) - 1));
  vector<comp> c(cnt), tmpa(cnt), tmpb(cnt);
  forn(i, cnt) c[i] = comp(i < sz(a) ? a[i] : 0, i < sz(b) ? b[i] : 0);
  fft(c, false);
  forn(i, cnt) {
    tmpa[i] = (c[i] + c[(cnt - i) % cnt].conj()) / comp(2, 0);
    tmpb[i] = (c[i] - c[(cnt - i) % cnt].conj()) / comp(0, 2);
  }
  forn(i, cnt) c[i] = tmpa[i] * tmpb[i];
  fft(c, true);
  vector<li> ans(cnt);
  forn(i, cnt) ans[i] = li(c[i].x + 0.5);
  return ans;
}

int pw[N];

void solve() {
  int cur = 1;
  forn(i, MOD) {
    pw[cur] = i;
    cur = mul(cur, 2);
  }
  sort(a, a + n, greater<int>());
  while (n > 0 && a[n - 1] == 0)
    --n;

  forn(i, n) a[i] = pw[a[i]];

  vector<int> A(MOD);
  forn(i, n)++ A[a[i]];
  vector<int> B = A;

  auto C = mul(A, B);

  li ans = 0;
  forn(i, 2 * MOD) ans += C[i] * binpow(2, i);
  forn(i, n) ans -= binpow(2, a[i] + a[i]);
  ans /= 2;
  ans = ans % MOD;
  printf("%lld\\n", ans);
}

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  //	freopen("output.txt", "w", stdout);

  int tt = clock();
#endif

  cerr.precision(15);
  cout.precision(15);
  cerr << fixed;
  cout << fixed;

  prepare();

#ifdef _DEBUG
  while (read()) {
#else
  if (read()) {
#endif
    solve();

#ifdef _DEBUG
    cerr << "TIME = " << clock() - tt << endl;
    tt = clock();
#endif
  }
}