1955. Count Number of Special Subsequences
On This Page
class Solution:
def countSpecialSubsequences(self, nums: List[int]) -> int:
mod = 10**9 + 7
n = len(nums)
f = [0] * 3
f[0] = nums[0] == 0
for i in range(1, n):
if nums[i] == 0:
f[0] = (2 * f[0] + 1) % mod
elif nums[i] == 1:
f[1] = (f[0] + 2 * f[1]) % mod
else:
f[2] = (f[1] + 2 * f[2]) % mod
return f[2]