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

/**
 *   Bismillahir Rahmanir Rahim.
 *   Imtiaz_rafi
 *   PCIU, CHITTAGONG
 **/
#include <bits/stdc++.h>
using namespace std;
#define speed()                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define file()                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define rep(i, a) for (ll i = 0; i < a; i++)
#define rep1(i, begin, end) for (ll i = begin; i <= end; i++)
#define trace(x) cerr << #x << " = " << x << endl
#define mem(dp, i) memset(dp, i, sizeof(dp))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define pf push_front
#define mp make_pair
#define F first
#define S second
#define pi acos(-1)
#define en '\\n'
// bitshift (1LL<<(3)) *2^3
typedef long long int ll;
typedef vector<ll> vi;
typedef map<ll, ll> mi;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef set<ll> st;
typedef set<char> sc;
ll dx[] = {-1, +1, 0, 0, +1, -1, +1, -1};
ll dy[] = {0, 0, -1, +1, +1, +1, -1, -1};
const ll mx = 1e5 + 123;
const ll MOD = 1e9 + 7;

template <class T> T power(T a, T b) {
  a %= MOD;
  if (!a)
    return 0;
  T p = 1;
  while (b > 0) {
    if (b & 1) {
      p *= a;
      p %= MOD;
    }
    a *= a;
    a %= MOD;
    b = b >> 1;
  }
  return p;
}
template <class T> void print(vector<T> &v) {
  for (T u : v) {
    cout << u << " ";
  }
  cout << endl;
}
template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); }
template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }

int main() {
  speed();
  ll a, i, j, sum = 0, ans = 0;
  cin >> a;
  vi v(a);
  rep(i, a) {
    cin >> v[i];
    sum += v[i] % MOD;
  }
  rep(i, a) {
    sum -= v[i];
    ans += (((sum % MOD) * (v[i] % MOD)) % MOD);
  }
  cout << ans % MOD << endl;
  return 0;
}


Error: TLE

Fixed submission:

/**
 *   Bismillahir Rahmanir Rahim.
 *   Imtiaz_rafi
 *   PCIU, CHITTAGONG
 **/
#include <bits/stdc++.h>
using namespace std;
#define speed()                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define file()                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define rep(i, a) for (ll i = 0; i < a; i++)
#define rep1(i, begin, end) for (ll i = begin; i <= end; i++)
#define trace(x) cerr << #x << " = " << x << endl
#define mem(dp, i) memset(dp, i, sizeof(dp))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define pf push_front
#define mp make_pair
#define F first
#define S second
#define pi acos(-1)
#define en '\\n'
// bitshift (1LL<<(3)) *2^3
typedef long long int ll;
typedef vector<ll> vi;
typedef map<ll, ll> mi;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef set<ll> st;
typedef set<char> sc;
ll dx[] = {-1, +1, 0, 0, +1, -1, +1, -1};
ll dy[] = {0, 0, -1, +1, +1, +1, -1, -1};
const ll mx = 1e5 + 123;
const ll MOD = 1e9 + 7;

template <class T> T power(T a, T b) {
  a %= MOD;
  if (!a)
    return 0;
  T p = 1;
  while (b > 0) {
    if (b & 1) {
      p *= a;
      p %= MOD;
    }
    a *= a;
    a %= MOD;
    b = b >> 1;
  }
  return p;
}
template <class T> void print(vector<T> &v) {
  for (T u : v) {
    cout << u << " ";
  }
  cout << endl;
}
template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); }
template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }

int main() {
  speed();
  ll a, i, j, sum = 0;
  cin >> a;
  vi v(a);
  rep(i, a) {
    cin >> v[i];
    for (j = 0; j < i; j++) {
      sum += (((v[i] % MOD) * (v[j] % MOD)) % MOD);
    }
  }
  cout << sum % MOD << endl;

  return 0;
}