#define _USE_MATH_DEFINES #include using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector a(n); for (auto& x : a) cin >> x; vector cnt1(n + 1), cnt2(n + 1); for (int i = 0; i < n; i++) { cnt1[i + 1] = cnt1[i] + (a[i] == 1); cnt2[i + 1] = cnt2[i] + (a[i] == 2); } long long ans = 0; for (int i = 0; i < n; i++) { if (a[i] == 1) { ans += 2 * cnt1[i]; ans += 3 * cnt2[i]; ans += 2 * (i - cnt1[i] - cnt2[i]); } else if (a[i] == 2) { ans += 3 * cnt1[i]; ans += 1 * cnt2[i]; ans += 1 * (i - cnt1[i] - cnt2[i]); } else { ans += 2 * cnt1[i]; ans += 1 * cnt2[i]; ans += 1 * (i - cnt1[i] - cnt2[i]); } } cout << ans << endl; return 0; }