#include <iostream>
using namespace std;
#define MOD 1000000007

int main(int argc, const char * argv[]) {
    int N;
    cin>>N;
    int *A=new int[N];
    long long s=0,s2=0;
    for(int i=0;i<N;++i){
        cin>>A[i];
        s+=A[i];
        s%=MOD;
        s2+=(long long)A[i]*A[i];
        s2%=MOD;
    }
    s*=s;
    s%=MOD;
    s-=s2;
    s%=MOD;
    if(s<0){
        s+=MOD;
    }
    if(s%2==0){
        cout<<(s/2)<<endl;
    }else{
        cout<<((s+MOD)/2)<<endl;
    }
    
    delete[] A;
    return 0;
}

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

#define int long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i < b; i++)
#define out(x) cout << x << endl
#define EPS (1e-7)
#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<bool> > vvb;
typedef vector<vector<double> > vvd;
typedef vector<vector<string> > vvs;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
typedef vector<vector<vector<ll> > > vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
//cout<<fixed<<setprecision(10);

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template<class T> void debug(T v){rep(i,v.size()) cout<<v[i]<<" ";cout<<endl;}
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
//以降 cin の入力元が 'input.txt' になる
//std::ifstream in("input.txt");
//std::cin.rdbuf(in.rdbuf());

const int max_n = 8100;
bool pos_x[max_n][max_n*2];
bool pos_y[max_n][max_n*2];
signed main()
{
    gearup;
    int n; cin >> n;
    vl a(n);
    rep(i,n)cin>>a[i];
    if(a[0] != 0){
        out(-1);
        return 0;
    }

    ll res = 0;
    rep(i,n-1){
        if(a[i+1] > i+1){
            out(-1);
            return 0;
        }
        if(a[i] + 1 == a[i+1])res++;
        else if(a[i] >= a[i+1])res += a[i+1];
        else {
            out(-1);
            return 0;
        }
    }
    out(res);
}