#include using namespace std; struct dat { int Count; long long totDis; dat() : Count(0), totDis(0) {} }; int main() { int N; cin >> N; long long Ans = 0; vector A(N); vector L(501), R(501); for (int i = 0; i < N; i++) { cin >> A.at(i); R.at(A.at(i)).Count++; R.at(A.at(i)).totDis += i; } for (int i = 0; i < N; i++) { for (int j = 1; j < 501; j++) { int diff = abs(A.at(i) - j); Ans += ((long long)i * L.at(j).Count - L.at(j).totDis) * diff; Ans += (R.at(j).totDis - (long long)i * R.at(j).Count) * diff; } R.at(A.at(i)).Count--; R.at(A.at(i)).totDis -= i; L.at(A.at(i)).Count++; L.at(A.at(i)).totDis += i; } cout << Ans << endl; }