#include <bits/stdc++.h>
using namespace std;
const long long N = 2e3 + 7;
const long long MOD = 1e9 + 7;
long long dp[N][N];
void maxi(long long& a, long long b) { a = max(a, b); }
long long add(long long a, long long b) {
  a += b;
  while (a >= MOD) a -= MOD;
  return a;
}
long long mult(long long a, long long b) {
  a *= b;
  a %= MOD;
  return a;
}
void solve() {
  long long n;
  cin >> n;
  vector<pair<long long, long long> > a(n);
  for (auto& x : a) cin >> x.first;
  for (long long i = 0; i < n; i++) {
    a[i].second = i;
  }
  sort((a).begin(), (a).end());
  reverse((a).begin(), (a).end());
  dp[0][n] = 0;
  for (long long i = 0; i < n; i++) {
    pair<long long, long long> cur = a[i];
    for (long long l = 0; l <= i; l++) {
      long long r = l + n - i;
      maxi(dp[l + 1][r], add(dp[l][r], mult(cur.first, llabs(cur.second - l))));
      maxi(dp[l][r - 1],
           add(dp[l][r], mult(cur.first, llabs((r - 1LL) - cur.second))));
    }
  }
  long long ans = 0;
  for (long long i = 0; i <= n; i++) {
    ans = max(ans, dp[i][i]);
  }
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  clock_t beg = clock();
  long long t = 1;
  while (t--) {
    solve();
  }
  cerr << "\nExecution time: " << (clock() - beg) / 1000 << '\n';
}
