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

#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  int n;
  scanf("%d", &n);
  long long num[n], sum[n];
  for (int i = 0; i < n; i++) {
    scanf("%lld", &num[i]);
    sum[i] = i == 0 ? num[i] : (sum[i - 1] % mod + num[i] % mod) % mod;
  }
  long long ans = 0;
  for (int i = n - 1; i > 0; i--) {
    ans = (ans % mod + (num[i] % mod * sum[i - 1] % mod) % mod) % mod;
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int i64;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int, int>> vii;
#define all(c) c.begin(), c.end()
#define clr(x, y) memset(x, y, sizeof x)
#define MOD 1000000007
#define eps 1e-9
#define INF 0x3f3f3f3f3f3f3f3f // 4,557,430,888,798,830,399
#define inf 0x3f3f3f3f         // 1,061,109,567
#define PI acos(-1.0)          // 3.1415926535897932
#define max3(x, y, z) max(max(x, y), z)
#define min3(x, y, z) min(min(x, y), z)
#define to_lower(x) transform(x.begin(), x.end(), x.begin(), ::tolower)
#define to_upper(x) transform(x.begin(), x.end(), x.begin(), ::toupper)
#define vin(x)                                                                 \\
  for (auto &m : x)                                                            \\
    cin >> m;
#define f first
#define s second
#define lcm(x, y) (x * y) / __gcd(x, y)

int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
int dx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
int dy[] = {-1, +1, +0, +0, +1, +1, -1, -1};
int hr[] = {1, 1, 2, 2, -1, -1, -2, -2};
int hc[] = {2, -2, 1, -1, 2, -2, 1, -1};

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

template <typename T> T power(T n, T p) {
  T res = 1;
  while (p > 0) {
    if (p & 1)
      res *= n;
    n *= n;
    p >>= 1;
  }
  return res;
}
template <typename T> T bigmod(T n, T p, T m) {
  n %= m;
  T res = 1;
  while (p > 0) {
    if (p & 1)
      res = res * n % m;
    n = n * n % m;
    p >>= 1;
  }
  return res;
}

const int mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed;
  cout << setprecision(6);
#ifndef ONLINE_JUDGE
  clock_t tStart = clock();
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  long long arr[n], sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++)
      sum = (sum % mod + (arr[i] * arr[j]) % mod) % mod;
  }
  cout << sum << '\\n';
#ifndef ONLINE_JUDGE
  fprintf(stderr, "\\n>> Runtime: %.10fs\\n",
          (double)(clock() - tStart) / CLOCKS_PER_SEC);
#endif
  return 0;
}