After the proposed solution, here should be the final one and now it passes all performance tests:
int result = 0;
int ones = 0;
int len = a.length;
for (int i = len - 1; i >= 0; i--) {
if (a[i] == 1) {
ones++;
} else {
result += ones;
}
}
return (result > 1000000000 || result < 0) ? -1 : result;