Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (llint i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define Pll pair<llint, llint>
#define keta(a) fixed << setprecision(a)
const ll mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
const ll INF = (1LL << 63);

int main() {
  llint n;
  cin >> n;
  hei(ll) a(n), s(n + 1, 0);
  llint sum = 0;
  rep(i, 0, n) { cin >> a[i]; }
  for (llint i = n - 1; i >= 0; i--) {
    s[i] = s[i + 1] + a[i];
    s[i] %= mod;
  }
  ll ans = 0;
  rep(i, 0, n) {
    ans += a[i] * s[i + 1] % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define Pll pair<llint, llint>
#define keta(a) fixed << setprecision(a)
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
const ll INF = (1LL << 63);

int main() {
  int n;
  cin >> n;
  hei(llint) a(n);
  llint sum = 0;
  rep(i, 0, n) {
    cin >> a[i];
    a[i] %= mod;
  }
  rep(i, 0, n - 1) {
    rep(j, i + 1, n) {
      sum += a[i] * a[j];
      sum %= mod;
    }
  }
  cout << sum << endl;
  return 0;
}