#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long n) {
  a %= 1000000007;
  if (n == 1) return a;
  if (n == 0) return 1;
  if (n % 2)
    return (a * (power((a * a) % 1000000007, n / 2) % 1000000007)) % 1000000007;
  return power((a * a) % 1000000007, n / 2) % 1000000007;
}
const long long inf = (long long)1e18;
long long inverse(long long x) { return power(x, 1000000007 - 2) % 1000000007; }
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  vector<pair<long long, long long> > v(n + 5);
  for (long long i = 1; i < n + 1; ++i) cin >> v[i].first, v[i].second = i;
  sort(v.begin() + 1, v.begin() + n + 1,
       greater<pair<long long, long long> >());
  long long dp[n + 5][n + 5];
  memset(dp, 0, sizeof dp);
  for (long long i = n; i > 0; --i) {
    for (long long len = 0; len < i; ++len) {
      long long last_occ = i - len - 1;
      dp[i][len] =
          max(abs(len + 1 - v[i].second) * v[i].first + dp[i + 1][len + 1],
              abs(n - last_occ - v[i].second) * v[i].first + dp[i + 1][len]);
      dp[i][len] %= 1000000007;
    }
  }
  cout << dp[1][0];
  return 0;
}
