#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vs = vector<string>;
const double pi = 3.14159265358979;
const ll mod = 1000000007;
int main() {
  ll N;
  cin >> N;
  vector<pair<int, int>> A(N);
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    A[i] = make_pair(a, i);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  ll dp[N + 1][N + 1];
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i + 1; j++) {
      dp[i + 1][j] =
          max(dp[i + 1][j],
              dp[i][j] + abs(N - i + j - A[i].second - 1) * A[i].first);
      dp[i + 1][j + 1] =
          max(dp[i + 1][j + 1], dp[i][j] + abs(A[i].second - j) * A[i].first);
    }
  }
  ll ans = 0;
  for (int i = 0; i <= N; i++) ans = max(ans, dp[N][i]) % mod;
  cout << ans % mod << endl;
}
