#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const long double Pi = acos(-1), e = 2.718;
const int N = 2e3 + 10, mod = 1e9 + 7;
vector<vector<long long>> dp;
vector<pair<long long, long long>> a;
int n;
long long solve(int i, int l, int r) {
  if (i == n) return 0;
  long long &ret = dp[l][r];
  if (ret + 1) return ret;
  ret = max(solve(i + 1, l + 1, r) + a[i].first * abs(a[i].second - l),
            solve(i + 1, l, r - 1) + a[i].first * abs(a[i].second - r));
  ret %= mod;
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  a.resize(n);
  dp.resize(n, vector<long long>(n, -1));
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a.begin(), a.end(), greater<pair<long long, long long>>());
  cout << solve(0, 0, n - 1) << '\n';
  return 0;
}
