[Question]
  [
Given an array of integers, count the number of contiguous subarrays with an even sum. You may assume that the array is non-empty, and contains only non-negative integers.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins.
### Test Cases
```
Input -> Output
[7] -> 0
[8] -> 1
[3, 5] -> 1
[6, 2] -> 3
[2, 4, 5] -> 3
[7, 4, 0, 5, 8] -> 6
[4, 8, 7, 9, 5] -> 7
[1, 9, 1, 0, 4] -> 7
[7, 5, 2, 1, 4, 8] -> 9
[0, 6, 6, 5, 3, 3, 8] -> 13
[8, 5, 9, 4, 5, 1, 0] -> 16
```
  
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), 57 bytes
```
f=lambda S,p=1:S>[]and f(S[1:])+sum((p:=p^s%2)for s in S)
```
[Try it online!](https://tio.run/##NVDZSsUwEH3PVwwBIdEot4vdJPcn2rexQi/XYqBLaKNgf76mUwPzcLaZTMb@uq95Sgq77Huvh2683TuoldVRVV@x7aY79KLGqGrl0/o9CmErbT/Wh1j28wIrmAlquTeac455C89XuDAsCEQMEwWvAWcKYsIJw1hBGixPc6IXryg4ezOGXikUeKsMyZxhRDSicBrEnBpj0tMwoWToMxmVdxOq/8X8kwWp5bnHOfH0Mv@Tl9UOxgn@PnHJGo34OHZWfP50g3LBO8JcyvY4gzvO0LTswEbNxKpN98LIN7uYyYlNbVrPcv8D "Python 3.8 (pre-release) – Try It Online")
Similar to brute-force method below but should have better complexity.
# [Python 3](https://docs.python.org/3/), 57 bytes
```
f=lambda S:S>[]and~sum(S)%2+f(S[1:])+f(S[:-1])-f(S[1:-1])
```
[Try it online!](https://tio.run/##NU/JasMwEL3rKwZBQWrlEi/1VpyfcG6qDiqpqcCWha0U4kN/3VXGFczhbbO5u/@ebb7vQzfq6fOqoW/7s1TaXn/X28R6/pS9DKyXaas4gjZJFU8O6QF3M7l58WBvk7uDXsE6cukopbJSkJzhRGSNICUyF/AWcSkgQ5wTmQkoohVohfQUFAFHb0lkUGoBwWpisiIyRZpiuIhihY0Z6kWc0BAZMiVWcHOs/8PCyhrV5rjjmHh4ZfjkdXWj8Yx@WMrDb1I@T9qxrx89Ch@9R5hyroZ5AQ/GwkWRBzZiRtZu3cAMf3eLsZ5tYuu6me9/ "Python 3 – Try It Online")
This is a brute force method based on inclusion-exclusion. Also works on Python2.
# [Python 3](https://docs.python.org/3/), 60 bytes
```
f=lambda S,e=0,i=0:e+(S>S[:i]and f(S,[e+1,i-e][S[i]&1],i+1))
```
[Try it online!](https://tio.run/##NU/JboMwEL37K0YcKrtMKhwoW@X8BLlNfaAKqJbAWIRWCj9PHVOkObxtNvdYviebbluvhnb8urXQYKcSNCqpu5g3l4Zqo1t7g543SF0s0Zw6TQ0Z/SI1mlgKsZnRTfMC9md0D2jvYB27qiiKqNBwukDCqAxAMkoR3g@cI5wDThmdEbLD8rQINPEKwt6bM/JKieCt6kgWjGSgMoSzQyxC4zno2TGhYuQzeSjvpqH@D/Mry6BW@x37xN3L/SdvdzeYhUefNhL@N6LXsXW8@20HXA7vGY6E0P00wwLGwlWzJzY4BVavqudGfLjZ2IWvuCo1ie0P "Python 3 – Try It Online")
This loops over the right ends of possible subarrays keeping the number of non empty even summing subarrays in `e`. The number of odd summing subarrays is implicit in `i-e` (`i` is the loop index). As we move `i` to the right we update `e` by incrementing if we pass through an even element and by replacing with the current odd summing number if we pass through an odd element. The sum of all `e`s is then output.
## Correctness and connection with @Bubbler's closed formula
This is a routine combinatorial partition. To count all (non-empty) contiguous subarrays of `S` with even sum (cswes) we split the set of all cswes into all cswes ending at `S(1)`, all cswes ending at `S(2)` etc. and add the sizes of these sets instead. If `e(n)` is the size of the set of cswes ending at `S(n)` then depending on the parity of `S(n+1)` we have `S(n+1)` even ~> `e(n+1)=e(n)+1` and `S(n+1)` odd ~> `e(n+1)=o(n)` where `o(n)` is the number of contiguous subarrays with odd sum ending at `S(n)`. Obviously, `o(n)+e(n)=n`. And with that we have got all the pieces used by the algorithm.
Now to connect with Bubbler's formula let us reinterpret what we are doing when going from `e(n)` to `e(n+1)`. If `S(n+1)` is even then we "increment a counter", more specifically, "the active counter". There is also an "inactive counter" which is, err, inactive. If `S(n+1)` is odd then we "swap the active and inactive counters". Which of the two counters is currently active, i.e. in `e` depends on the cumulative parity of `S` up to the current position. The counter that started out as `e(0)` therefore counts from `0` to `n0` (excl) where `n0` is as defined by Bubbler and the counter that started out as `o(0)` counts from `0` to `n1`. The algorithm adds all the steps together. Therefore, using the well-known summation formula `1+2+...+n = n(n+1)/2` we recover Bubbler's formula.
For reference, here is Bubbler's relevant comment in full:
> 
> Fastest algorithm: prepend 0, cumulative sum, each number mod 2, then count zeros (n0) and ones (n1) and calculate n0\*(n0-1)/2+n1\*(n1-1)/2. Probably not so appealing for golf though. –
> Bubbler
> 
> 
> 
[Answer]
# [Python 2](https://docs.python.org/2/), 57 bytes
```
z=1
for x in input():z+=1j+1-x%2*2*z%1j
print abs(z*z)//4
```
[Try it online!](https://tio.run/##JU3bCoMwDH33K4IgeOlwqZ1WwS8RH7bhUBEV51jXn3cxhROSc0my/vZ@meXx7YepA6w60z193z9sjd5r2cDAMBPWzx5GlU1qHBO8mEDGMrYBjt66DfMO98c7tLGN0lQd53ZTtF6jqTIBN2q5AElNClBOKHi6EhFw5ohoAaSWzkeekCPK5SkpWVJuhZycQUbGOFXNvHSP3IX2Dw "Python 2 – Try It Online")
Thanks to dingledooper for saving 2 bytes with `z%1j` in place of `z.real`!
**58 bytes**
```
z=p=1
for x in input():p*=1|x%-2;z+=1j+p
print abs(z*z)//4
```
[Try it online!](https://tio.run/##JU3bCoMwDH33K4Iw8NLhWjtvo18iPmzDoTK0uI51Zf/uYgonJOeSRH/NsMxi@wzjswfe9La/h2G4OaUVDx7LChbGGaHfJoobnSj@s4ejuLhU8SnVgV7H2cD19opc4uIsk9u@3pZd0FZYOYMztoKBwCYYSC@UNJ2QMNhzSCoGqNbe5zRxikifx6QgSfoVdAoCGjlhVyvitX/kL3R/ "Python 2 – Try It Online")
**59 bytes**
```
t=e=o=0
for x in input():e+=1;exec"e,o=o,e;"*x;t+=e
print t
```
[Try it online!](https://tio.run/##JU3bCoMwDH33K0KftpkHW5230i8Rn0aGwrBFKnNf79IUTkjOJUn4xcVv5vou64dAj3TSSyl1RUfOu6p4@x1OWDdGOOLtPlLptJUUoXceyarHaWPpqAj7ukWIV9qfurmYeq4a4cmtRTDcDEKThU6miglCyjHpEVgdsq9l0hJpcp6TRqQmr7DTCtioBUnthQ/5Ub4w/wE "Python 2 – Try It Online")
**59 bytes**
```
n=0;a=1
for x in input():n+=1;a+=1-x%2*2*a
print a*a+n*n>>2
```
[Try it online!](https://tio.run/##JU3bCoMwDH33K4Iw2GoHNjqv6I@ID2U4FEYUccx9fZemcJrkXNJsv2NeCd13Xt4TmGY6p2ccx466tLWdiV7rDicsxNg@x/XWUNKZ1nK5nxdUqGy07QsdYJVNSFHfo/P7QzlGQ8Uv0/DgVmhAbqghD0IpU8pEg88xqTSwWgffyGQkkoc8J1GkPKywUwjYyARerYTX4VD4YfwD "Python 2 – Try It Online")
**59 bytes**
```
p,t=l=[0,0]
for x in input():l[p]+=1;p^=x%2;t+=l[p]
print t
```
[Try it online!](https://tio.run/##JU3bCoMwDH33K4Iw2DAPbXValX5J6V5Gh4Jokcrc17s0hROSc0kSfnHaVnV9p3nxIAd/@ndZllfAaBZjBQpXfLYdTphXQjji/TEsNrjKyDG8zHlTY6xMUoqwz2uEeKV927nCaqoa4UmtRVDUFEKThY4nQQQh5YhoBFL77EueJEeanKekYqnJK@S0DDJqRlI18z4/yhfcHw "Python 2 – Try It Online")
All these solutions iterate through the list just once. They're based on [Bubbler's observation](https://codegolf.stackexchange.com/questions/237700/even-sum-subarrays#comment539157_237700) that the output can be expressed in terms of the numbers \$(e,o)\$ of even and odd entries in the cumulative sum as:
$$ \frac{e(e+1)+o(o-1)}{2}$$
This is because a sub-list has even sum if and only if its start and end indices correspond to elements of the same parity in the cumulative sum. The number of ways to choose two even or two odd elements in the cumulative sum is \$\binom{c}{2}\$, that is \$c(c-1)/2\$, where \$c\$ is their count. For evens, we need to either prepend a `0` to the cumulative sum or increase the count by 1 to account for the initial total being 0 which is even.
---
# [Python](https://docs.python.org/3/), 52 bytes
```
f=lambda l,b=-1:l>[]and~sum(l)%2+f(l[1:],0)+f(l[:b])
```
[Try it online!](https://tio.run/##JU3NCoMwDL7vKXoZtKwDW50/BfcipYeKikLsZNONXfbqXUxLSr6/JOt3mx4hj3FswS9d7xnIrr0qA3frfOh/r33hIM76MnKwyjiZCYKmcyJ@phkGpsz6nMPGRz68PfA5rPvGBb5oK3eyNf5cshu2UjKNTUtWJKEilCGR7MghqSVDtUm@IqQoUqQ8JjVJRRpBp6RCI6c61Jp4kw6lDe4P "Python 3 – Try It Online")
Inspired by [loopy walt's inclusion-exclusion approach](https://codegolf.stackexchange.com/a/237703/20260). Thanks to loopy walt for -2 bytes!
We want to recursively reach all contiguous sublists of `l` to count those with even sum. We do this by repeatedly removing either the start (first) or end (last) element. But, this generates duplicate sublists because they can be reached via removals in different orders, like start then end versus end then start.
So, we guarantee that we do all start removals before any end removals. That is, once we remove the end, we no longer remove the start. We use a flag `b` for whether start removals are allowed, which starts at -1 and is set to 0 after an end removal. When `b` is 0, end removals are replaced with removing the entire list, which effectively cuts off that branch. This gives a quadratic runtime.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 4 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ŒOÈO
```
[Try it online](https://tio.run/##yy9OTMpM/f//6CT/wx3@//9Hm@uY6BjomOpYxAIA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfeX/o5P8D3f4/9f5Hx1tHqsTbQHExjqmQNJMxwhIGumYgHnmQNpAx1QHJG@iY6FjrmMJFjcE0oZAGROwGlMdIyDPBKzKQMcMCE11jIEQxLcAsi1BpoHUx8YCAA).
**Explanation:**
```
Œ     # Get the sublists of the (implicit) input-list
 O    # Sum each inner list
  È   # Check for each whether it's even
   O  # Take the sum to get the amount of truthy results
      # (after which this is output implicitly as result)
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), ~~15~~ 13 [bytes](https://github.com/abrudz/SBCS)
```
+/~2|∊+\¨,⍨\⎕
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qKM97b@2fp1RzaOOLu2YQyt0HvWuiHnUNxUk8z@Ny5wrjcsCiI0VTIGkmYIRkDRSMAHzzIG0gYKpAkjeRMFCwVzBEixuCKQNgTImYDWmCkZAnglYlYGCGRCaKhgDIYhvAWRbgkwDqQcA "APL (Dyalog Unicode) – Try It Online")
`,⍨\⎕`: Reversed prefixes of the input.  
`+\¨`: For each reversed prefix, get the cumulative sums.  
`∊`: Flatten into a vector of sums.  
`~2|`: For each sum, is it even?  
`+/`: Take the sum.
Slightly longer, but more interesting alternative:
```
+/,∘.(≤⍱2|-)⍨+\2+0,⎕
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qKM97b@2vs6jjhl6Go86lzzq3WhUo6v5qHeFdoyRtoHOo76pIDX/07jMudK4LIDYWMEUSJopGAFJIwUTMM8cSBsomCqA5E0ULBTMFSzB4oZA2hAoYwJWY6pgBOSZgFUZKJgBoamCMRCC@BZAtiXINJB6AA "APL (Dyalog Unicode) – Try It Online")
`0,⍵`: Prepend a 0 to the input.  
`2+`: Add 2 to each number. This makes sure all values are positive while not changing any subsequences parities.  
`+\` Take the cumulative sum. Because the vector contained only positive numbers this is strictly increasing.  
`∘.( )⍨`: Make a table by applying the inner function between all pairs of values.  
`+/,`: Flatten the table and sum all values.
The inner function uses NOR (`⍱`) and might be a bit easier to understand if rewritten with AND (`∧`):
```
(a≤b)⍱(2|a-b) ≡ (~a≤b)∧(~2|a-b) ≡ (a>b)∧(a-b is even)
```
The `a>b` makes sure each subsequence is only counted once.
[Answer]
# [Haskell](https://www.haskell.org/), 44 bytes
```
f a=sum[gcd 2s-1|s<-scanl1(+)=<<scanr(:)[]a]
```
[Try it online!](https://tio.run/##NU5LboMwEN37FG9RKUY1UgyUn3BP0BtYKHIDoVYIqTDd9e50GIo0i/ebN/Plwr0fx3W9wZnw87DDtUMSYv0bmjhc3TRq@RqZptnwLOvItq5dH85PMOieAhjQxBj65cNPPdGxXyC9utSX@hlR5nPu3R3SmFN8ijBQ4nv204IX3CDJ6@ApZsBw69u6V1u0iN9xFrZkoIVNFd4OnCskjFNhE4XssIgWTM@kKOy7ubCklApkVUeyEFYz1RzODrHgxYT17GiohKVMzkNuyvP/GJ0sWa32P/bG3cv/AA "Haskell – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~59 ...~~ 37 bytes
```
->l{*q=r=0,-1;l.sum{|n|q[r^=n%2]+=1}}
```
[Try it online!](https://tio.run/##VY/NDoIwEITvPsUmxosWQ39sIaa8CKkHD5yQSA0HAzx7bXaLDcle5pvZ6dZPz2/obCiafj6P1tuSFfzeXz/Ta16GZWz9ww4n4S6Wr2t4Q9e2xrkjFA2UB5RVkpykZHDbE81AJCKJCAYqxxI0CMvIGWydmqzIKwYxUOctQxZHyHFR7S2DVQJdlTtrcmNe48SMxPl/JB1UoVfTrfTGltDhBw "Ruby – Try It Online")
Finally found the time to implement Bubbler's advice.
[Answer]
# x86-64 machine code, 21 bytes
```
31 c9 f7 e1 ff c1 f6 07 01 74 02 87 d1 01 c8 af ff ce 75 f0 c3
```
[Try it online!](https://tio.run/##XVFRS8MwEH7PrzgqQluyqahDnPNB8F1kL7Lt4ZakbTRNS9LJ6NhftybpbJnhSL77vst9F8ImOWNddyE1Uzsu4Mk2XFbT4pmcUUpuPUfQlhCTaK2nuaq2qEBYi5EnPHgMCNzaVwYE21O/DWS5U0NuRC2wGS84s7PaRtgGXj6Wr/C2fIeV4XJD4WaQP1uwX7Ie/ViRnwx538TLY3vkHAT@m8cytHzIuHATWDla6Bb6KQfKCIeTOSFSN@HlsQcpUvCn@lNKlDooaHJGgRVoIE1d8p2QA@lf68TVBhZwuKYwC3FP4TbEw3EeimrjyrI4upRrHdHez1lZ2YoqizG5OqEUk8RZH0nX/bBMYW67STm7c5v7rYXrIdQv)
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes the address of an array of 32-bit integers in RDI and the length of the array in RSI.
Assembly:
```
.text
.global essa
.intel_syntax noprefix
essa:
    xor ecx, ecx    #
    mul ecx         # Initialise ECX, EAX, and EDX to 0.
repeat:
                    # EAX holds the running total of even-sum subarrays.
                    # ECX holds the number of even-sum subarrays ending at the current position.
                    # EDX holds the number of odd-sum subarrays ending at the current position.
    inc ecx                 # Increment the even count.
    test BYTE PTR [rdi], 1  # Check the low bit of the current number.
    jz skip                 # Jump if the low bit is 0.
    xchg ecx, edx           # (Executed if odd) Swap the counts.
skip:
    add eax, ecx            # Add the even count to the running total.
    scasd                   # Advance to the next number, and perform an unnecessary comparison.
    dec esi                 # Count down from the length of the array.
    jnz repeat              # Jump back if there is more to be processed.
    ret                     # Return.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
Ẇ§2ḍS
```
[Try it online!](https://tio.run/##y0rNyan8///hrrZDy40e7ugN/h9toWOqY6ljAiQNdQxiD7f/BwA "Jelly – Try It Online")
Similar to my Vyxal answer, but Jelly's builtins are nicer. A monadic link.
```
Ẇ     # Sublists
 §    # Sum (vectorised)
  2ḍ  # Is divisisible by 2? (vectorised)
    S # Sum (non-vectorising)
```
[Answer]
# [J](http://jsoftware.com/), 17 bytes
```
1#.1#.(0=2|+/)\\.
```
[Try it online!](https://tio.run/##TY69DsIwDIT3PoUFQ6kwIX@kSaVMSExMzN0QVcXCC/DuwdhKVMkZ7r7zxe@yU/0CeYIeEDRM9E4Kro/7rZi9ojnobL/H8zDPqgxd93quH8pkWGAUYVjErXAIF9GOdUCwW20RfIsEKWNLk4u1a2RAbsQ/Tm1DgGHL8JIXkGoVlVhmvrUZ@ZrCgYcijqdyuSMySXKg1HflBw "J – Try It Online")
* `(0=2|+/)` Is sum even...
* `\\.` For each suffix list of each prefix list (so all sublists)? Returns a 0-1 matrix, and now we want to sum all the elements...
* `1#.` Sum the rows...
* `1#.` And sum the resulting list of sums.
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~8~~ 7 bytes
```
#ȯ¬%2ΣQ
```
[Try it online!](https://tio.run/##yygtzv7/X/nE@kNrVI3OLQ78//9/tIGOGRCa6hgDoUUsAA "Husk – Try It Online")
#### Explanation
```
      Q  # consecutive subsequences
#        # count elements by
 ȯ       # composed function
  ¬      # not
     Σ   # sum
   %2    # mod 2
```
-1 bytes thanks to [ovs](https://codegolf.stackexchange.com/users/64121/ovs)
[Answer]
# JavaScript (ES6), ~~44~~ 41 bytes
This is inspired by [Bubbler's comment](https://codegolf.stackexchange.com/questions/237700/even-sum-subarrays#comment539157_237700), but updates the sum \$n\_0(n\_0-1)/2+n\_1(n\_1-1)/2\$ on the fly rather than computing it afterwards. The number of 0's and 1's are stored in `a[0]` and `a[1]` respectively, with `a[]` initialized to `[1,0]`.
```
a=>a.map(c=>t+=a[p^=c&1]++,a=[1,p=t=0])|t
```
[Try it online!](https://tio.run/##fdDBDoIwDAbgu0@xk4EwgQFucBgvQjBZEI0G2SKLJ999zhKjjunS49d/bc/iJqbuelJ6M8p9bw7cCF6L@CJU0PFaR1w0ase7NWmjCAveEKy45mkb3rXp5DjJoY8HeQwOQcPaMETLlyQoXTm0/EmJS3OMth7toxSjzE9zl2YYFZ5gH2VAU6sx@pjbUupS60qMbEP1lW0pcykBRCC4@E8ZfJ2BLt4zWFq51IZRKNuQQ4F@HmuxVwmqmg8xj/Ky1DwA "JavaScript (Node.js) – Try It Online")
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/mathematica/), ~~36~~ 34 bytes
```
Tr[1-Mod[Tr/@Subsequences@#,2]]-1&
```
–2 bytes from @alephalpha
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n277P6Qo2lDXNz8lOqRI3yG4NKk4tbA0NS85tdhBWccoNlbXUO1/QFFmXolDenS1eW0sF5xjgcwx1lEwReab6SgYIfONdBRM0JSYg4UMgKI6CihmAUUtdBSA0pZoOgzBQoZgTSZoRgENMQLLmaCZBlRrBkZAFcZgBJRWQPIGWMYS4j6I6UD5/wA)
There is an `EvenQ` test in Mathematica, but alephalpha's use of `Mod` saves 2 bytes relative to it:
```
Count[Tr/@Subsequences@#,_?EvenQ]-1&
```
Alternative 34-byte function from @att:
```
Count[{a__/;2∣+a}]@*Subsequences
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 5 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
ãx èv
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=43gg6HY&input=W1s3XSxbOF0sWzMsIDVdLFs2LCAyXSxbMiwgNCwgNV0sWzcsIDQsIDAsIDUsIDhdLFs0LCA4LCA3LCA5LCA1XSxbMSwgOSwgMSwgMCwgNF0sWzcsIDUsIDIsIDEsIDQsIDhdLFswLCA2LCA2LCA1LCAzLCAzLCA4XSxbOCwgNSwgOSwgNCwgNSwgMSwgMF1dLW0)
```
ãx      - subarrays reduced
   èv   - number of evens
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~50 47~~ 44 bytes
```
g l=sum[1|t<-scanr(:)[]l>>=scanl1(+),even t]
```
[Try it online!](https://tio.run/##TY7LboMwEEX3/YorlIUtGzUGyiMKfEG76pKiCqVWUtU4KDhtF/l3auwaRZrNuWfmak799CWVmucjVD1dh1bczD6eDr2@kB1tO9U09UJKEEa5/JYappuH/lOjxtCPL@8Yr@bVXJ412geQ6XT@gaKMRYgbRIy5ZHP8zx4hf0d5MPIDu9VCLu5NRzeiuKT72BYBpC06ji3lHkoLIkDK8XTPOUdiOQ2ccGR@ZY0KF21tyrF05UHYtOSwuvIXRRDCRcIdZfeicCWJc5lvq4Kzu7kbu5G6cY@vb5TOVP4/3774nHbd/Ac "Haskell – Try It Online")
* thanks to @ovs for reminding me there's already an *even* function in Haskell (3 Bytes saved).
* saved 3 more bytes inspired by @Lynn [answer](https://codegolf.stackexchange.com/a/237741/84844) and [tip for golfing in Haskell](https://codegolf.stackexchange.com/a/52945/84844)
```
sum          Total of
[1|t<-       * list comprehension counting:
scanr(:)[]l    -tails
>>=            - concatMapped to
scanl1(+)      - cumulative sum
,even t]       ? that satisfy the requirements 
```
Old recursive version
```
g[]=0
g l=sum[1|t<-scanr1(+)l,even t]+g(init l)
```
```
g[]=0            empty list has 0 even sums
g l=             list has :
sum              > number of results found in:
even t             valid tails
t<-scanr1(+)l      directly reduced by +
+g(init l)       > + results of inits
```
[Answer]
# [R](https://www.r-project.org/), ~~61~~ 57 bytes
Or **[R](https://www.r-project.org/)>=4.1, 50 bytes** by replacing the word `function` with `\`.
*-4 bytes thanks to [@Dominic van Essen](https://codegolf.stackexchange.com/users/95126/dominic-van-essen).*
```
function(a,b=diffinv(a)%%2,`+`=sum,z=+!b,o=+b)0:z+0:o-z-o
```
[Try it online!](https://tio.run/##VY9NCsMgEEb3PYWlBBQnYEyqScCeJT9FyKIKbdNFLm/tmDQEZjPvfTOOz2BNsLMb35N3tIfB3CdrJ/ehPcsyCR3vzGt@wGL4eQBv@MBEu3DR@nzJfbB0pJqxC8lvRJx@Xb12BXYlkOsBKCByBSUCCaTaQ4lpZCJiINs@hSbiGkj0zT6j0RTIChyrDkbjHomy2hc2KGNaYcVIifX/QLqlRtWkK9MDW0CFLw "R – Try It Online")
Using @Bubbler's formula.
Without it:
### [R](https://www.r-project.org/), 64 bytes
Or **[R](https://www.r-project.org/)>=4.1, 57 bytes** by replacing the word `function` with `\`.
```
function(a){for(i in seq(!a))for(j in 1:i)F=F+!sum(a[j:i])%%2;F}
```
[Try it online!](https://tio.run/##VY/BCoMwDIbve4rIEFrWga3aqsMdfYmxg8gKFaZM52ns2bua6kTIJd/3J00Hq0urp655m74jNf3ofiAGTAfj40WCmtIZtDPghaFVWZ2CcXqS@tYW5k7DUFyqr9WkIYrSI5yvEB3mLls6jl3MIN0ByUAsIEYgGCRbyDOFLHKYwbpPonE4Y@B8vs0oNBwZx7FkZxTuESiTbWGO0qUllovEWP8P@FsyVLm/0j@wBqT9AQ "R – Try It Online")
[Answer]
# [R](https://www.r-project.org/), 45 bytes
```
function(a,x=table(diffinv(a)%%2))x%*%(x-1)/2
```
[Try it online!](https://tio.run/##VY9BCoMwEEX3PcVACSQlUhNtYhb2LtY2IBSFYou3T9MfbRBmM@/9mUxewbfBv8d@HqaRd3Jp5@72fPD74P0wfngnGNNCLOzE@FIocdbB855bIY5UXKk8/Lpm7RS6StJlB4wkvYIKQEuqcygxC1ZGLGnbZ2AibiRF7/KMhVFgCmP1zljs0ZB1XuggY9qgYqRC/T@QbmmgXLoyPbAFTPgC "R – Try It Online")
Uses Bubbler's observation and pajonk's test harness.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `s`, 6 bytes
```
ÞSḢƛ∑₂
```
[Try it Online!](https://lyxal.pythonanywhere.com?flags=s&code=%C3%9ES%E1%B8%A2%C6%9B%E2%88%91%E2%82%82&inputs=%5B8%2C%205%2C%209%2C%204%2C%205%2C%201%2C%200%5D&header=&footer=)
Thanks to Lyxal for this version.
```
ÞS     # Sublists
  Ḣ    # Remove the empty list at the start
   ƛ   # Map to...
    ∑₂ # Even sum?
       # (s flag) sum of all
```
# [Vyxal](https://github.com/Vyxal/Vyxal), 8 bytes
```
ÞS'∑₂;L‹
```
[Try it Online!](https://lyxal.pythonanywhere.com?flags=&code=%C3%9ES%27%E2%88%91%E2%82%82%3BL%E2%80%B9&inputs=%5B8%2C%205%2C%209%2C%204%2C%205%2C%201%2C%200%5D&header=&footer=)
```
ÞS       # Sublists (Including empty array :( )
  '  ;   # Filtered by...
   ∑     # Sum...
    ₂    # Is even
      L‹ # Length of this -1 (Because empty array)
```
[Answer]
# [Perl 5](https://www.perl.org/) + `-pal`, 56 bytes
```
$"="+";$_=map{//;grep!(1&eval"@F[$'..$_]"),$_..$#F}0..@F
```
[Try it online!](https://tio.run/##FYzNCsIwEITv@xRrDP6gpkmatCml0FNfQiT0EESINlTxIr66ccvA8DEzTApztDlz1rEDa7nv7mP6FEV7nUNa7dQmvMfI@uHMt0Jwf2H7I/dE6@ErheiHnGtwUKKFCjVoNEQ1uUSLDgw6rLGhTJErSg21FjUxdSCxIlksSQ4cUbM8LMvflF636fHMpzTGPw "Perl 5 – Try It Online")
---
# [Perl 5](https://www.perl.org/) + `-pal -MList::Util+(sum)`, 46 bytes
```
$_=map{//;grep!(1&sum@F[$'..$_]),$_..$#F}0..@F
```
[Try it online!](https://tio.run/##FY5PC4JAFMTv71O8aCmlXP@urkbgyVMdO0WIBwlBc1E7RV@9bWRg@DHzeIxpp15ZK@rz0JiP75@eU2s2Trib30NZ3cVeSlE/3KOoAdvqG0hZVtZmpClmRSlHFHECyuABK9aUsOaMc2QhPESaoFUcgdFRwCmkOIY0aVC@flgvf6NZuvE1W880vfWul25eiuK2dP3BwSD3Dw "Perl 5 – Try It Online")
[Answer]
# [ayr](https://github.com/ZippyMagician/ayr), 11 bytes
Thanks to [ovs's](https://codegolf.stackexchange.com/a/237720/90265) method.
```
+/|2|,+\\.I
```
# Explanation
`I` is the input (eg `6 2`) passed through the command line.
```
        \.  Suffixes decreasing in length
      +\    Cumulative sum of each
     ,      Flatten
   2|       Mod 2
  |         Not this (is the number divisible by 2?)
+/          Sum; count number of even elements
```
[Answer]
# TI-Basic, 27 bytes
```
fPart(.5cumSum(Ans
{2sum(Ans),1+sum(not(Ans
sum(Ans.5(Ans-1
```
Uses [Bubbler's formula](https://codegolf.stackexchange.com/questions/237700/even-sum-subarrays#comment539157_237700). Takes input in `Ans`. Output is stored in `Ans` and displayed.
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), ~~42~~ 27 bytes
```
.\B|\D
Y`d`01
Cw`(0|10*1)+
```
[Try it online!](https://tio.run/##K0otycxLNPyvquGe8F8vxqkmxoWLKzIhJcHAkMu5PEHDoMbQQMtQU/v//2jzWK5oCyA21lEwBVJmOgpGQMpIR8EEImAOZhkAOToKIHVAjoWOAlDUEiJvCGYZgpWYQNQDVRqBhUwgWoAyZmAElDAGI5CoBZhvCbEIYkIsAA "Retina – Try It Online") Link includes test cases. Edit: Saved 15 bytes thanks to @m90. Explanation:
```
.\B|\D
```
Delete anything that's not the last digit of an integer.
```
Y`d`01
```
Reduce modulo 2.
```
Cw`(0|10*1)+
```
Count the number of overlapping matches with an even number of `1`s.
[Answer]
# JavaScript (Node.js), 89 bytes
```
f=x=>0 in x&&f(x.slice(1))+x.filter((_,i)=>~x.slice(0,i+1).reduce((s,y)=>s+y,0)&1).length
```
[Try it online!](https://tio.run/##VcnRCsIgFIDh@z3FuRoeZjJHUBDuRSJibLoM0aE23E2vbhJE7PL//uewDmH0eokH6yaZsxJJ9C1oC6muFUksGD1KwhGbxJQ2UXpC7lSj6N@/2VLdcGReTq9SJNCt3NBstMW6uJF2jo@8Dh4Wr20EAaOzwRnJjJsvVfVVosj1dEP813lXnEK3g47CsUD@AA "JavaScript (Node.js) – Try It Online")
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 47 bytes
```
a->sum(i=1,#a,sum(j=i,#a,!(vecsum(a[i..j])%2)))
```
[Try it online!](https://tio.run/##LY3rCoMwDIVfJXMMGogyq/PyQ19E@qOMOSpuFHeBPX2XxkJIzvly0nq7ufzuwwxDsPn4@jyUG0o6WopyGVyUB/W9XaO3kyuKxeBJI2Kw3q8/ZSEfwW/u@WaZRZPBrCwiwTS1hnsXW0VwibMh0HFqgjqhVuSZHYFk2XUEjPuUKEWWEqrTCYe1sDpd8a6R4k0lJbgT0O/f7Y8Yg@EP "Pari/GP – Try It Online")
[Answer]
# [Factor](https://factorcode.org/) + `math.unicode`, 34 bytes
```
[ all-subseqs [ Σ even? ] count ]
```
[Try it online!](https://tio.run/##TY9BbsIwEEX3nOL3AERxbJJSJLqs2HRTdYVYuO60jTBOsJ2qFHEa7sOV0iFSauTVezP@M/OhTWx8//qyen56QKB9R85QyOgneh2w0/Er61xtmncaAJ/kyGtb/@pYNy5gS96RTT/Reorx0PraRSwmkyMqnHCH6RI5w/0IgkFidoslihElYwGV6nIIUshZ/WeULBVjhXnqrFgKFoJ71a2suKdgrVLAnHXOc0suSX5pOznsOuOc6xLXrLFS9mtoa6ehe@OjA9a4nEHf5B6xgWk6PnvTCwGn29YekAUYS9r3fw "Factor – Try It Online")
[Answer]
# Scala, 52 bytes
```
_.tails.flatMap(_.inits)count(x=>x.size>0&x.sum%2<1)
```
[Try it online!](https://scastie.scala-lang.org/IpRl4HwDREa4FVXZntPAlw)
This is a naive answer, but it didn't seem as if Bubbler's approach would be shorter, so I went with this. `_.tails.flatMap(_.inits)` first gets all subarrays that stretch to the end, then all the prefixes of those subarrays to get all subarrays of the original array.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~23~~ 20 bytes
```
ＩΣＥ²↨¹…№﹪Ｅ⊕Ｌθ↨¹…θλ²ι
```
[Try it online!](https://tio.run/##RYvBCsIwEER/ZY8biKBFQfFmToIF0WPpYUmXppAmbZoIfn2M9eDAwMC8pw0F7cnmfA@Di6hoifhMI9Y0YSXhQgvjTsKDXM@ofCpM7btk/UpcnQ48sovc4Y1dHw3OQvw19daWlfETzhLs96lKB/HLOeemOUo4SDhJ2K@jSNu2zZuX/QA "Charcoal – Try It Online") Link is to verbose version of code. Explanation: Uses @Bubbler's formula.
```
   ²                    Literal `2`
  Ｅ                     Map over implicit range
            θ           Input list
           Ｌ            Length
          ⊕             Incremented
         Ｅ              Map over implicit range
                θ       Input list
               …        Truncated to length
                 λ      Inner index
             ↨¹         Converted from base 1 i.e. summed
        ﹪               Vectorised modulo
                  ²     Literal `2`
       №           ι    Count the number of `0`s or `1`s
      …                 Exclusive range from 0
    ↨¹                  Converted from base 1 i.e. summed
 Σ                      Take the sum
Ｉ                       Cast to string
                        Implicitly print
```
Each contiguous subarray with an even sum must start and end at a point where the cumulative sums are either both even or both odd. The positions of these sums don't matter, only their counts do. The number of sums for a given count is then simply `n(n-1)/2`, since a given sum can't start and end at the same point. This is readily calculated as the sum of the exclusive range from `0` to `n`. (Both these sums are implemented as base conversion from base `1` since that returns `0` for an empty list whereas `Sum` returns `None`.)
[Answer]
# Excel, ~~130~~ 97 bytes
*-33 bytes applying Bubbler's formula*
```
=LET(x,COUNT(A1#),a,SEQUENCE(x),b,SUM(MOD(MMULT(A1#,(a<=TRANSPOSE(a))*1),2)),b*(b-1-x)+(x^2+x)/2)
```
[Link to Spreadsheet](https://1drv.ms/x/s!AkqhtBc2XEHJnW7R4Ai3XDnmVNZr?e=EAGuNE)  
Rearranged Bubbler's formula to use the number of 1s and size of the original array (instead of the number of 0s). Multiplies the array by an upper triangular matrix to calculate the cumulative sums.
## Original Answer not using Bubbler's method
```
=LET(x,COUNT(A1#),a,SEQUENCE(1,x^2)-1,b,MOD(a,x)+1,c,INT(a/x)+1,d,SEQUENCE(x),SUM(1-MOD(MMULT(A1#,FILTER((d>=b)*(d<=c),b<=c)),2)))
```
### Explanation
Since Excel formulas don't really have loops, I have to get creative with in sequences in two dimensional space.  
`LET(x,COUNT(A1#),` : x = number of elements  
`a,SEQUENCE(1,x^2)-1,` : a = [0..x^2]  
`b,MOD(a,x)+1,` : b = array of indices of the first items to be summed  
`c,INT(a/x)+1,` : c = array of indices of the last items to be summed  
`d,SEQUENCE(x),` : d = [1..x]  
`FILTER((d>=b)*(d<=c),b<=c))` : array containing all permutations of possible consecutive sums indicated by 1 in the elements to be summed  
`MMULT(A1#,~,2)` : use matrix multiplication to determine all the sums of consecutive elements  
`SUM(1-MOD(~,2)))` : count the sums where the sum mod 2 = 0
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~77~~ \$\cdots\$ ~~67~~ 65 bytes
```
t;p;f(a)int*a;{for(int b[]={1,t=p=0};~*a;)t+=b[p^=*a++&1]++;t=t;}
```
[Try it online!](https://tio.run/##bZTbjpswEIbv9ykspFScomLO1KW9qPoUhK1YAlu0W4KCpUaN6KOXDrY57LARyeD5fs8/TCzK43NZjiNnHav1wmhabhbsXl@uOtySpyxP79TmaZc6A/sLyOBW@pR1j6lZWNYHmlsW4ylnwzjJfxVNqxvk/kDgMyV41fMfNMtJSu6RTY50YGvelfkY5z2Z92wSYORLFNrExSiQCPL@OxvDpQegjhDsfCOp8QUCZfJOnVhqqKBUlPKxJlm8AtEnFZqdHXWkDkqE4gK1J669lC6jCoSzfETpP0nngZtC30u13GqrYavoqeirGKgYqhipGKuY2HO3883GTmRa6dY3f6pLrUuN8VEtTbW2yZa7iLuIe4h7iPuI@4gHiAeIh4iHiEeIR4jHiMeIJ4gniFMHD8jBit0IqbGZe3XrqpJX5@UEUXHJ4xOK0ysPMIU1DdXO8tL2nJQ/i6sJv1X5Ul1lAe10@@6ebsk3@AaaTbZrT1O74Z1AxEuhac/VDbY5TN1@nluf21p7XzKMWJZQz6@H9cRCKXlqBc/ZFpNW0XZH135UL9BHK2y2FtOnu4Ks1rVDfzjD0zVfp0f8pGUQeNbkxlpzeOPdQ2H4P4y3HVWQXeaPm5qtcnL8Qg5ncuhPLdj09jLwPk2r2XJ4GMZ/Zf1aPPfj8fd/ "C (gcc) – Try It Online")
*Saved ~~9~~ 11 bytes thanks to [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)!!!*
Inputs a pointer to an array of non-negative integers terminated by \$-1\$ (because pointers in C carry no length info).  
Returns the number of contiguous subarrays with an even sum.
Port of [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)'s [JavaScript answer](https://codegolf.stackexchange.com/a/237733/9481).
[Answer]
## Batch, 91 bytes
```
@set/at0=t=e=0,t1=-1
@for %%a in (%*)do @set/ae^^=%%a%%2&call set/at+=t%%e%%+=1
@echo %t%
```
Takes input as command-line arguments. Explanation: Rough port of @Arnauld's JavaScript answer, but starting with `[0, -1]` as preincrement is golfier.
```
@set/at0=t=e=0,t1=-1
```
Start with a cumulative total of `0`, a result of `0`, 1 even cumulative total and 0 odd cumulative totals, but with the totals decremented so that they can be preincremented.
```
@for %%a in (%*)do 
```
Loop over the command-line arguments.
```
@set/ae^^=%%a%%2
```
Update the parity of the cumulative total. (`^&1` would also work for the same byte count.)
```
&call set/at+=t%%e%%+=1
```
Increment the count of even or odd cumulative totals respectively and add the new value to the final total. This has to use `call` and `%%` so that `e` gets substituted after its calculation above.
```
@echo %t%
```
Output the final total.
[Answer]
# [Haskell](https://www.haskell.org/), 64 bytes
```
f a=sum[div(d*d-d)2|d<-[sum[1|b<-scanl(+)0a,c b]|c<-[even,odd]]]
```
[Try it online!](https://tio.run/##NU5LboMwEN37FG9RKdDaEr/wiaAn6A0sFBnsUCsEqkCz4u50MEWaxfvNm/lW0930/breoKrp9yG1fXn6XQvtR4suhdy0cGlKMbVq6L0PP1C8RVMvLZnmZQY@al3X9fpQdkAFPTKgQynQmfnLDoZob2Z4ll8v18voU6Z5GnWHV1UncfLRUeLnaYcZb7jBI0/DUqyCg1vf1r3KrIb4RMBk7kDIZMxxPnDKETkcMxlxJIdFNHM0IIVj302ZJCXnIKs4khmToaOhCyeHmLnFyOnJ0VAwSZnUDbmxm//H6GTu1GL/Y2/cvfQP "Haskell – Try It Online")
]