#pragma GCC target("sse4") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include unsigned char a[200000]; int n; using ll = long long; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int temp; scanf("%d", &temp); a[i] = 1 << std::min(temp - 1, 2); } ll ans_v[8] = {}; for (int j = 0; j < n; j++) { unsigned char t = a[j]; int ans_v2[8] = {}; for (int ii = 0; ii < j/20;ii++) { int i = ii * 20; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; ++ans_v2[t | a[i++]]; } for (int i = j; i % 20 != 0;) { ++ans_v2[t | a[--i]]; } for (int i = 0; i < 8; i++) { ans_v[i] += ans_v2[i]; } } ll ans = 0; for (int i = 0; i < 8; i++) { ans += ans_v[i] * __builtin_ffs(~i); } printf("%lld\n", ans); }