<p>You are given a binary array <code>nums</code>.</p>

<p>A subarray of an array is <strong>good</strong> if it contains <strong>exactly</strong> <strong>one</strong> element with the value <code>1</code>.</p>

<p>Return <em>an integer denoting the number of ways to split the array </em><code>nums</code><em> into <strong>good</strong> subarrays</em>. As the number may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,0,0,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are 3 ways to split nums into good subarrays:
- [0,1] [0,0,1]
- [0,1,0] [0,1]
- [0,1,0,0] [1]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is 1 way to split nums into good subarrays:
- [0,1,0]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1</code></li>
</ul>

from collections import deque
from typing import List

class Solution:
    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:
        # If there are no 1s in the array, it's impossible to form a good subarray
        if 1 not in nums:
            return 0
        
        # Initialize the answer and a counter for the number of zeros between 1s
        ans, digits = 1, 1

        # Use a deque to efficiently remove leading and trailing zeros
        nums = deque(nums)
        
        # Remove leading zeros as they don't affect the number of good subarrays
        while nums[0] == 0:
            nums.popleft()
        
        # Remove trailing zeros as they don't affect the number of good subarrays
        while nums[-1] == 0:
            nums.pop()

        # Iterate through the modified array
        for num in nums:
            if num == 1:
                # Multiply the current answer by the number of zeros (digits) since the last 1
                ans *= digits
                # Apply modulo operation to keep the result within bounds
                ans %= 1_000_000_007
                # Reset the digits counter after encountering a 1
                digits = 0

            # Increment the digits counter for each zero encountered
            digits += 1

        return ans
1713