alexjercan/bugnet huggingface dataset:
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  ll mod = 1e9 + 7;
  int n;
  cin >> n;
  vector<ll> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  ll presum = 0;
  for (int i = 0; i < n; i++) {
    presum = (presum % mod + v[i] % mod) % mod;
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    presum = (presum % mod - v[i] % mod + mod) % mod;
    sum = (sum % mod + (v[i] * (presum % mod)) % mod) % mod;
  }

  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  ll mod = 1e9 + 7;
  int n;
  cin >> n;
  vector<ll> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  ll sum = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += (v[i] * v[j]) % mod;
    }
  }
  cout << sum % mod << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, i1, n) for (int i = (int)(i1); i < (int)(n); i++)
#define ll long long
#define vi vector<int>
#define vll vector<long long>

int main() {
  ll n;
  cin >> n;
  vector<ll> vec(n);
  ll sumall = 0;
  rep(i, 0, n) {
    cin >> vec.at(i);
    sumall += vec.at(i);
  }

  ll sum = 0;

  rep(i, 0, n) {
    sumall -= vec.at(i);
    sum += vec.at(i) * (sumall % 1000000007);
    if (sum >= 1000000007) {
      sum %= 1000000007;
    }
  }
  cout << sum;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, i1, n) for (int i = (int)(i1); i < (int)(n); i++)
#define ll long long
#define vi vector<int>
#define vll vector<long long>

int main() {
  ll n;
  cin >> n;
  vector<ll> vec(n);
  rep(i, 0, n) { cin >> vec.at(i); }

  ll sum = 0;
  ll a = 0;

  rep(i, 0, n) {
    rep(j, i + 1, n) {
      a = vec.at(i) * vec.at(j);
      if (a >= 1000000007) {
        sum += a % 1000000007;
      } else {
        sum += a;
      }
    }
  }
  if (sum >= 1000000007) {
    sum %= 1000000007;
  }
  cout << sum;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<long long>> vvll;
typedef long double ld;
typedef std::vector<long double> vld;
typedef std::vector<std::vector<long double>> vvld;
using namespace std;
void yesno(bool state);

void Main() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];

  ll MOD = 1e9 + 7;

  ll sum = 0;
  ll memo = 0;
  rep(i, N - 1) {
    memo += A[i];
    memo %= MOD;
    sum += (memo * A[i + 1]) % MOD;
    sum %= MOD;
  }

  cout << sum << endl;
}

int main(int argc, char const *argv[]) {
  Main();
  return 0;
}

void yesno(bool state) {
  if (state)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<long long>> vvll;
typedef long double ld;
typedef std::vector<long double> vld;
typedef std::vector<std::vector<long double>> vvld;
using namespace std;
void yesno(bool state);

void Main() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];

  ll MOD = 1e9 + 7;

  ll sum = 0;
  rep(i, N) rep(j, i) {
    sum += (A[i] * A[j]) % MOD;
    sum %= MOD;
  }

  cout << sum << endl;
}

int main(int argc, char const *argv[]) {
  Main();
  return 0;
}

void yesno(bool state) {
  if (state)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

const ll mod = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  vector<ll> B(N + 1, 0);
  for (int i = 0; i < N; i++) {
    B[i + 1] = B[i] + A[i];
  }

  ll sum = 0;
  for (int i = 0; i < N - 1; i++) {
    sum += A[i] * ((B[N] - B[i + 1]) % mod);
    sum %= mod;
  }

  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main() {
  ll N, ans = 0;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += A[i] * A[j];
      if (ans > 1000000007)
        ans %= 1000000007;
    }
  }

  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main(void) {
  int n;
  cin >> n;

  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    // cout << a[i] << " ";
  }
  // cout << endl;

  vector<ll> b(n - 1);
  b[0] = a[0];
  // cout << b[0] << " ";
  rep(i, n - 2) {
    b[i + 1] = (b[i] + a[i + 1]) % 1000000007;
    // cout << b[i+1] << " ";
  }
  // cout << endl;

  int ans = 0;
  rep(i, n - 1) {
    ans += a[i + 1] * b[i] % 1000000007;
    ans %= 1000000007;
    // cout << a[i+1] << " " << b[i] << " " << ans << endl;
  }
  // cout << endl;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main(void) {
  int n;
  cin >> n;

  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }

  int ans = 0;
  rep(i, n - 1) {
    rep(j, n - i - 1) {
      int tmp;
      tmp = a[i] * a[i + j + 1] % 1000000007;
      ans += tmp;
      ans %= 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define MOD 1000000007
#define M_PI 3.14159265358979323846 /* pi */
#define sec second
#define fir first
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repe(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define entire(a) (a).begin(), (a).end()
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
void solve() {
  ll n;
  cin >> n;
  std::vector<ll> v(n);
  std::vector<ll> s(n);
  rep(i, n) cin >> v[i];
  s[0] = v[0];
  repe(i, 1, n) { s[i] = (s[i - 1] + v[i]) % MOD; }
  ll sum = 0;
  // rep(i,n)
  // cout<<s[i]<<" ";
  // cout<<endl;
  repe(i, 1, n) {
    // if(i==0)
    // sum=(v[i]*s[i])%MOD;
    // else
    sum += (v[i] * s[i - 1]) % MOD;
    sum %= MOD;
  }
  cout << sum << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define MOD 1000000007
#define M_PI 3.14159265358979323846 /* pi */
#define sec second
#define fir first
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repe(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define entire(a) (a).begin(), (a).end()
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
void solve() {
  int n;
  cin >> n;
  ll res = 0;
  std::vector<ll> v(n);
  rep(i, n) cin >> v[i];
  rep(i, n) {
    repe(j, i + 1, n) { res += (v[i] * v[j]) % MOD; }
    res %= MOD;
  }
  cout << res << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);

  long long sum = 0;
  long long u = 0;
  for (int i = n - 1; i > 0; i--) {
    u += a.at(i);
    u = u % 1000000007;
    sum += u * a.at(i - 1);
    sum = sum % 1000000007;
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);

  long long sum = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += (long long)a.at(i) * a.at(j);
      sum = sum % 1000000007;
    }
  }

  cout << sum << endl;

  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

long long mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<long long> v(n);
  vector<long long> f(n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int item;
    cin >> item;
    v[i] = item;
    sum += item;
    sum %= mod;
    f[i] = sum;
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    long long add = f[n - 1] - f[i] + mod;
    ans += (v[i] * add) % mod;
    ans %= mod;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include "cpp/Solution.h"

using namespace std;
long long mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<long long> v(n);
  for (auto &it : v) {
    cin >> it;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      long long add = (v[i] * v[j]) % mod;
      ans += add;
      ans %= mod;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (auto &i : v)
    cin >> i;
  ll int partSum = 0;
  for (int i = 0; i < n; i++) {
    partSum += v[i] % mod;
    partSum %= mod;
  }
  ll int totSum = 0;
  for (int i = 0; i < n - 1; i++) {
    partSum -= v[i] % mod;
    partSum = (partSum + mod) % mod;
    totSum += ((v[i] % mod) * (partSum % mod)) % mod;
    totSum %= mod;
  }
  cout << totSum << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (auto &i : v)
    cin >> i;
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    long long partSum = 0;
    for (int j = i + 1; j < n; j++) {
      partSum += v[j] % mod;
      partSum %= mod;
    }
    sum += ((partSum % mod) * (v[i] % mod)) % mod;
    sum %= mod;
  }
  cout << sum << '\\n';
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ull = unsigned long long int;
using ll = long long int;
constexpr ull mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ull> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  ull sum = 0;
  ull state = 0;
  for (int i = n - 2; i >= 0; i--) {
    state += A[i + 1];
    state = state % mod;
    sum += state * A[i];
    sum = sum % mod;
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ull = unsigned long long int;
using ll = long long int;
constexpr ull mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ull> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  ull sum = 0;
  for (int i = 0; i < n - 1; i++) {
    ull t = 0;
    for (int j = i + 1; j < n; j++) {
      t += A[j];
    }
    t = t % mod;
    sum += A[i] * t;
    sum = sum % mod;
  }

  cout << sum << endl;

  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(1000010);

int main() {
  int n;
  cin >> n;
  vector<mint> a(n), s(n + 1);
  rep(i, n) cin >> a[i];
  rep(i, n) s[i + 1] = a[i] + s[i];
  mint ans = 0;
  rep(i, n - 1) ans += a[i] * (s[n] - s[i + 1]);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      ll t = (a[i] % mod) * (a[j] % mod) % mod;
      ans = (ans + t) % mod;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
const int mod = 1e9 + 7;
const int INF = 2e9;
const int MAX = 1e6;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
const int mod = 1e9 + 7;
const int INF = 2e9;
const int MAX = 1e6;

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) { cin >> a[i]; }
  ll sum = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += (ll)a[i] * (ll)a[j];
      sum %= mod;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll INF = 1e18;
const int mod = 1e9 + 7;
const int MAX = 1e6;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

struct edge {
  int to, cost;
}; // 辺
// vector<edge> graph[MAX]; // 隣接リスト
// bool visit[MAX]; // 訪問の有無

ll power(int m, int n) { // m^n（繰り返し自乗法）
  if (n == 0)
    return 1;
  if (n % 2 == 1)
    return power(m, n - 1) * m % mod;
  ll r = power(m, n / 2);
  return r * r % mod;
}

ll inv(ll a, ll b) { // 逆元を利用してa/bのmodを返す
  return a * power(b, mod - 2) % mod;
}

int n; // 変数
ll a[MAX];
ll sum = 0;
ll ans = 0;

int main() {
  // input
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  // solve
  rep(i, n) {
    ans += (sum - a[i]) % mod * a[i] % mod;
    sum -= a[i];
    ans %= mod;
  }
  // ans = inv(ans, 2);
  // output
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll INF = 1e18;
const int mod = 1e9 + 7;
const int MAX = 1e6;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

struct edge {
  int to, cost;
}; // 辺
// vector<edge> graph[MAX]; // 隣接リスト
// bool visit[MAX]; // 訪問の有無

int n; // 変数
ll a[MAX];
ll sum = 0;

int main() {
  // input
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  // solve
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ll r = a[i] * a[j];
      r %= mod;
      sum += r;
      sum %= mod;
    }
  }
  // output
  cout << sum << "\\n";
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const ll mod = 1000000007;
int main() {
  int N;
  cin >> N;
  ll A[N];
  ll sum = 0LL;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    sum += A[i];
    sum %= mod;
    sum = (sum + mod) % mod;
  }
  ll ans = 0LL;
  for (int i = 0; i < N; i++) {
    sum -= A[i];
    sum = (sum + mod) % mod;
    ans += A[i] * sum % mod;
  }
  ans %= mod;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const ll mod = 1000000007;
int main() {
  int N;
  cin >> N;
  ll A[N];
  ll sum = 0LL;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      sum += A[i] * A[j] % mod;
      sum %= mod;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
// #define mod 1000000007
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define PI 3.1415926535897932384626
#define INF 1000000000
#define bits(n) __builtin_popcount(n)

const int M = 1e9 + 7;
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
void solve() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll pref[n];
  pref[0] = a[0];
  for (int i = 1; i < n; i++) {
    pref[i] = pref[i - 1] + a[i];
  }
  ll res = 0;
  for (int i = 0; i < n - 1; i++) {
    res = add(res, mul(a[i], pref[n - 1] - pref[i]));
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input1.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output1.txt", "w", stdout);
#endif

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define mod 1000000007
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define PI 3.1415926535897932384626
#define INF 1000000000
#define bits(n) __builtin_popcount(n)

ll fun(ll a[], ll n) {
  ll res = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      res += (a[i] * a[j]) % mod;
    }
  }
  return res;
}
void solve() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << fun(a, n) % mod;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input1.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output1.txt", "w", stdout);
#endif

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
  long long N, i;
  long long A[220000];
  long long p_sum[220000];
  long long ans = 0;
  long long tmp_mod = 0;
  cin >> N;

  for (i = 0; i < N; i++)
    cin >> A[i];

  p_sum[0] = A[0];

  for (i = 1; i < N; i++)
    p_sum[i] = p_sum[i - 1] + A[i];

  for (i = 0; i < N - 1; i++) {
    tmp_mod = (p_sum[N - 1] - p_sum[i]) % 1000000007;
    ans = ans + A[i] * tmp_mod;
    ans = ans % 1000000007;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
  long long N, i, j;
  long long A[220000];
  long long ans = 0;
  long long tmp_mod = 0;
  long long tmp = 0;
  cin >> N;

  for (i = 0; i < N; i++)
    cin >> A[i];

  for (i = 0; i < N - 1; i++) {
    for (j = i + 1; j < N; j++) {
      tmp = (A[i] * A[j]);
      tmp_mod = tmp % 1000000007;
      ans = ans + tmp_mod;
      if (ans > 1000000000)
        ans = ans % 1000000007;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 13;
const int mod = 1e9 + 7;
int a[maxn];
ll b[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    b[i] = (b[i - 1] + 1ll * a[i]) % mod;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += 1ll * a[i] * (b[n] - b[i] + mod) % mod;
    ans %= mod;
  }
  printf("%lld", ans % mod);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 13;
const int mod = 1e9 + 7;
int a[maxn];
ll b[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    b[i] = b[i - 1] + a[i];
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = (ans + a[i] * (b[n] - b[i]) % mod) % mod;
  }
  printf("%lld", ans);
}