Chef is a big fan of soccer! He loves soccer so much that he even invented soccer for dogs! Here are the rules of soccer for dogs:
- N$N$ dogs (numbered 1$1$ through N$N$) stand in a line in such a way that for each valid i$i$, dogs i$i$ and i+1$i + 1$ are adjacent.
- Each dog has a skill level, which is either 1$1$ or 2$2$.
- At the beginning of the game, Chef passes a ball to dog 1$1$ (dog 1$1$ receives the ball).
- For each valid i$i$, if dog i$i$ has skill level s$s$, this dog can pass the ball to any dog with number j$j$ such that 1≤|i−j|≤s$1 \le |i-j| \le s$.
- Each dog (including dog 1$1$) may receive the ball at most once.
- Whenever a dog receives the ball, it must either pass it to another dog or finish the game by scoring a goal.
While the dogs were playing, Chef also created a game for developers. He defined the result of a game of soccer for dogs as the sequence of dogs which received the ball in the order in which they received it. The last dog in the sequence is the dog that decided to score a goal; if a dog never received the ball, it does not appear in the sequence. In the game for developers, you should find the number of possible results of soccer for dogs.
Find this number of possible results modulo 109+7$10^9 + 7$. Two results of soccer for dogs (sequences of dogs' numbers) are considered different if these sequences have different lengths or if there is a valid index i$i$ such that the i$i$-th dog in one sequence is different from the i$i$-th dog in the other sequence.

-----Input-----
- The first line of the input contains a single integer T$T$ denoting the number of test cases. The description of T$T$ test cases follows.
- The first line of each test case contains a single integer N$N$.
- The second line contains N$N$ space-separated integers A1,A2,…,AN$A_1, A_2, \ldots, A_N$ denoting the skill levels of the dogs.

-----Output-----
For each test case, print a single line containing one integer - the number of different results of soccer for dogs, modulo 109+7$10^9 + 7$.

-----Constraints-----
- 1≤T≤10$1 \le T \le 10$
- 1≤N≤105$1 \le N \le 10^5$
- 1≤Ai≤2$1 \le A_i \le 2$ for each valid i$i$

-----Subtasks-----
Subtask #1 (10 points): N≤10$N \le 10$
Subtask #2 (30 points): N≤103$N \le 10^3$
Subtask #3 (60 points): original constraints

-----Example Input-----
3
4
1 1 1 1
3
2 2 2
4
1 2 1 1

-----Example Output-----
4
5
6

-----Explanation-----
Example case 1: The following results are possible: 1$1$, [1,2]$[1, 2]$, [1,2,3]$[1, 2, 3]$, [1,2,3,4]$[1, 2, 3, 4]$.
Example case 2: The following results are possible: [1]$[1]$, [1,2]$[1, 2]$, [1,2,3]$[1, 2, 3]$, [1,3,2]$[1, 3, 2]$, [1,3]$[1, 3]$.

Difficulty: VERY_HARD
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
for _ in range(int(input())):
	num = int(input())
	arr = list(map(int, input().split()))
	dp = [0] * len(arr)
	dpr = [0] * len(arr)
	dpt = [0] * len(arr)
	dp[0] = 1
	dpr[0] = 0
	ans = 1
	for i in range(1, len(arr)):
		j = i + 1
		count = 1
		dp[i] = dp[i - 1]
		if i - 2 >= 0 and arr[i - 2] == 2:
			dp[i] += dp[i - 2]
			if i - 3 >= 0 and arr[i - 3] == 2:
				dp[i] += dp[i - 3]
		ans += dp[i]
		if arr[i - 1] == 2 and i < len(arr) - 1:
			while j < len(arr) and arr[j] == 2:
				j += 1
				count += 1
			if j == len(arr):
				ans += dp[i - 1] * (count - 1)
			elif count % 2 != 0:
				if j < len(arr) - 1 and arr[j + 1] == 2:
					ans += dp[i - 1] * (count + 1)
				else:
					ans += dp[i - 1] * count
			elif count % 2 == 0:
				ans += dp[i - 1] * (count - 1)
	print(ans)


try:
	for _ in range(int(input())):

		class mat(object):
			game = []
			limit = []
			arr = []
			num = 0
			ans = 0

		def passes(n, m):
			if m > mat.num:
				return
			mat.ans += 1
			mat.limit.append(m)
			for i in range(1, mat.arr[m] + 1):
				if m - i > 0 and n < mat.num - 1 and (not m - i in mat.limit):
					passes(n + 1, m - i)
				if m + i < mat.num and n < mat.num - 1 and (not m + i in mat.limit):
					passes(n + 1, m + i)
			mat.limit.pop()
		mat.num = int(input())
		mat.arr = [] * mat.num
		mat.arr = list(map(int, input().split()))
		passes(0, 0)
		print(mat.ans)
except:
	pass


M = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	gwalani = [[0 for _ in range(n + 1)] for __ in range(4)]
	even = 0
	ans = 0
	odd = 0
	eveOne = 0
	oddOne = 0
	if n == 1:
		print(1)
		continue
	gwalani[0][2] = gwalani[0][1] = 1
	for i in range(3, n + 1):
		gwalani[0][i] = (gwalani[0][i - 1] + gwalani[1][i - 1]) % M
		if a[i - 3] > 1:
			gwalani[1][i] = (gwalani[0][i - 2] + gwalani[1][i - 2] + gwalani[2][i - 2]) % M
		if a[i - 3] > 1:
			gwalani[2][i - 1] += (gwalani[0][i - 2] + gwalani[1][i - 2]) % M
			gwalani[2][i - 1] %= M
		if a[i - 3] > 1 and i > 4 and (a[i - 2] > 1) and (a[i - 5] > 1):
			if i % 2 == 1:
				even = (even + gwalani[0][i - 4] + gwalani[1][i - 4]) % M
				ans += even
			else:
				odd = (odd + gwalani[0][i - 4] + gwalani[1][i - 4]) % M
				ans += odd
		elif i % 2 == 1:
			even = 0
		else:
			odd = 0
		if i > 3 and a[i - 1] > 1 and (a[i - 4] > 1):
			if i % 2 == 0:
				eveOne += gwalani[0][i - 3] + gwalani[1][i - 3]
				eveOne %= M
				ans += eveOne
				ans %= M
			else:
				oddOne += gwalani[0][i - 3] + gwalani[1][i - 3]
				oddOne %= M
				ans += oddOne
				ans %= M
		elif i % 2 == 0:
			eveOne = 0
		else:
			oddOne = 0
	for i in range(4):
		for j in range(1, n + 1):
			ans = (ans + gwalani[i][j]) % M
	print(ans)


for _ in range(int(input())):
	m = 1000000007
	n = int(input())
	ar = list(map(int, input().split()))
	dp1 = [0] * n
	dp2 = [0] * n
	if n == 1:
		print(1)
	else:
		dp1[0] = dp1[1] = 1
		for i in range(2, n):
			dp1[i] += dp1[i - 1]
			if ar[i - 2] == 2:
				dp1[i] = (dp1[i] + dp1[i - 2]) % m
			if ar[i - 2] == 2 and i - 3 >= 0 and (ar[i - 3] == 2):
				dp1[i] = (dp1[i] + dp1[i - 3]) % m
		p1 = 0
		p2 = 0
		for i in range(1, n - 1):
			if ar[i - 1] == 2:
				if i % 2 == 0:
					if p1 <= i:
						p1 = i + 1
						while p1 < n - 2 and ar[p1] == 2 and (ar[p1 + 1] == 2):
							p1 = p1 + 2
					dp2[i] = dp1[i - 1] * (p1 - i) % m
					if p1 < n - 1 and ar[p1 + 1] == 2:
						dp2[i] = (dp2[i] + dp1[i - 1]) % m
				else:
					if p2 <= i:
						p2 = i + 1
						while p2 < n - 2 and ar[p2] == 2 and (ar[p2 + 1] == 2):
							p2 = p2 + 2
					dp2[i] = dp1[i - 1] * (p2 - i) % m
					if p2 < n - 1 and ar[p2 + 1] == 2:
						dp2[i] = (dp2[i] + dp1[i - 1]) % m
		ans = 0
		for i in dp1:
			ans = (ans + i) % m
		for i in dp2:
			ans = (ans + i) % m
		print(ans)


import sys
sys.setrecursionlimit(10 ** 8)
try:
	for _ in range(int(input())):

		class mat(object):
			limit = []
			arr = []
			num = 0
			ans = 0

		def passes(n, m):
			if m > mat.num:
				return
			mat.ans += 1
			mat.limit.append(m)
			for i in range(1, mat.arr[m] + 1):
				if m - i > 0 and n < mat.num - 1 and (not m - i in mat.limit):
					passes(n + 1, m - i)
				if m + i < mat.num and n < mat.num - 1 and (not m + i in mat.limit):
					passes(n + 1, m + i)
			mat.limit.pop()
		mat.num = int(input())
		mat.arr = [] * mat.num
		mat.arr = list(map(int, input().split()))
		passes(0, 0)
		print(mat.ans)
except:
	pass


m = int(1000000000.0 + 7)
for t in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	dp = [0] * (n + 1)
	dp1 = [0] * (n + 1)
	dp[0] = 1
	dp[1] = 1
	for i in range(2, n):
		if i >= 3 and ar[i - 2] == 2 and (ar[i - 3] == 2):
			dp[i] = ((dp[i - 1] + dp[i - 2]) % m + dp[i - 3]) % m
		elif ar[i - 2] == 2:
			dp[i] = (dp[i - 1] + dp[i - 2]) % m
		else:
			dp[i] = dp[i - 1]
	p1 = 2
	p2 = 1
	ans = dp[0]
	if n > 1:
		ans = (ans + dp[n - 1]) % m
	for i in range(1, n - 1):
		if i % 2 == 0:
			if ar[i - 1] == 2:
				if i >= p1:
					p1 = i + 1
					while p1 + 2 < n and ar[p1] == 2 and (ar[p1 + 1] == 2):
						p1 += 2
				if p1 + 1 < n and ar[p1 + 1] == 2:
					dp1[i] = (p1 - i + 1) * dp[i - 1] % m
				else:
					dp1[i] = (p1 - i) * dp[i - 1] % m
		if i % 2 != 0:
			if ar[i - 1] == 2:
				if i >= p2:
					p2 = i + 1
					while p2 + 2 < n and ar[p2] == 2 and (ar[p2 + 1] == 2):
						p2 += 2
				if p2 + 1 < n and ar[p2 + 1] == 2:
					dp1[i] = (p2 - i + 1) * dp[i - 1] % m
				else:
					dp1[i] = (p2 - i) * dp[i - 1] % m
		ans = (ans + (dp[i] + dp1[i]) % m) % m
	print(ans)


T = int(input())
for t in range(T):
	n = int(input())
	arr = [int(y) for y in input().split()]
	if n == 1:
		print(1)
		continue
	x = 1
	x_prev = 0
	paths = 2
	ex = 1
	ex_prev = 1
	ex_prev2 = 0
	sprev_2 = 2
	ptx_xprev = 0
	ptxprev_x = 1
	for i in range(2, n):
		if arr[x_prev] == 1:
			if arr[i] == 1:
				ptx_xnext = ex
				ptxnext_x = 0
				ex_next = ex
				paths = (paths + ptx_xnext) % 1000000007
			else:
				ptx_xnext = (ex + ptx_xprev) % 1000000007
				ptxnext_x = 0
				ex_next = ex
				paths = (paths + ptx_xnext) % 1000000007
		elif arr[i] == 1:
			ptxnext_x = ptxprev_x
			ptx_xnext = ex
			ex_next = (ex + ex_prev) % 1000000007
			if sprev_2 == 2:
				ex_next = (ex_next + ex_prev2) % 1000000007
			paths = (paths + ptx_xnext + ptxnext_x + ex_next - ex) % 1000000007
		else:
			ptxnext_x = ptxprev_x
			ptx_xnext = (ex + ptx_xprev) % 1000000007
			ex_next = (ex + ex_prev) % 1000000007
			if sprev_2 == 2:
				ex_next = (ex_next + ex_prev2) % 1000000007
			paths = (paths + ptx_xnext + ptxnext_x + ex_next - ex) % 1000000007
		ex_prev2 = ex_prev
		ex_prev = ex
		ex = ex_next
		ptx_xprev = ptxnext_x
		ptxprev_x = ptx_xnext
		sprev_2 = arr[x_prev]
		x_prev = x
		x = i
	print(paths)


def array1(size):
	return [0 for _ in range(size)]

def array2(rows, cols):
	return [[0 for _ in range(cols)] for _ in range(rows)]
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	no_of_twos = array1(n + 1)
	for i in range(n - 1, -1, -1):
		if a[i] == 1:
			no_of_twos[i] = 0
		else:
			no_of_twos[i] += 1
			no_of_twos[i] += no_of_twos[i + 1]
	dp = array1(n + 1)
	dp[0] = 1
	ans = 1
	for i in range(1, n):
		if i - 1 >= 0:
			dp[i] += dp[i - 1]
		if i - 2 >= 0 and a[i - 2] == 2:
			dp[i] += dp[i - 2]
		if i - 3 >= 0 and a[i - 3] == 2 and (a[i - 2] == 2):
			dp[i] += dp[i - 3]
		dp[i] = dp[i] % 1000000007
		ans += dp[i]
		x = no_of_twos[i + 1]
		if i + 1 < n and a[i - 1] == 2:
			ans += x * dp[i - 1]
			if x % 2 == 0:
				if i + x + 1 < n:
					ans += dp[i - 1]
				if i + x + 2 < n and a[i + x + 2] == 2:
					ans += dp[i - 1]
		ans = ans % 1000000007
	print(ans % 1000000007)


import math
from math import log2, sqrt
import sys
sys.setrecursionlimit(pow(10, 6))
import collections
from collections import defaultdict
from statistics import median
inf = float('inf')
mod = pow(10, 9) + 7

def give(l, n, index, visited):
	if index < 0 or index >= n:
		return 0
	if visited[index] == 1:
		return 0
	visited[index] = 1
	theta = 1
	for i in graph[index]:
		theta += give(l, n, i, visited.copy())
	return theta
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	visited = defaultdict(int)
	graph = defaultdict(set)
	for i in range(len(l)):
		if l[i] == 1:
			graph[i].add(i - 1)
			graph[i].add(i + 1)
		else:
			graph[i].add(i - 2)
			graph[i].add(i - 1)
			graph[i].add(i + 1)
			graph[i].add(i + 2)
	theta = give(l, n, 0, visited.copy())
	print(theta)


try:
	for _ in range(int(input())):

		class mat(object):
			game = []
			limit = []

		def see(key):
			for i in range(len(mat.limit) - 1, -1, -1):
				if mat.limit[i] == key:
					return False
			return True

		def passes(n, m, arr):
			if m > len(arr) or n > len(arr):
				return
			mat.game[n][m] += 1
			mat.limit.append(m)
			for i in range(1, arr[m] + 1):
				if m - i > 0 and n < len(arr) - 1 and (not m - i in mat.limit):
					passes(n + 1, m - i, arr)
				if m + i < len(arr) and n < len(arr) - 1 and (not m + i in mat.limit):
					passes(n + 1, m + i, arr)
			mat.limit.pop()
		n = int(input())
		arr = list(map(int, input().split()))
		mat.game = [[0] * len(arr) for _ in range(len(arr))]
		passes(0, 0, arr)
		ans = 0
		for i in mat.game:
			ans += sum(i)
		print(ans)
except:
	pass


LIMIT = 10 ** 9 + 7

def paths(dogs, used, cur):
	if cur in used or cur < 0 or cur >= len(dogs):
		return 0
	dog = dogs[cur]
	if dog == 1:
		l = paths(dogs, used | {cur}, cur + 1)
		r = paths(dogs, used | {cur}, cur - 1)
		return l + r + 1
	else:
		ll = paths(dogs, used | {cur}, cur + 2)
		rr = paths(dogs, used | {cur}, cur - 2)
		l = paths(dogs, used | {cur}, cur + 1)
		r = paths(dogs, used | {cur}, cur - 1)
		return ll + rr + l + r + 1

def get_multis(dogs):
	multis = [0] * len(dogs)
	cur = 1
	ahead = 0
	while cur < len(dogs):
		score = 0
		if multis[cur] == 0 and dogs[cur - 1] == 2:
			ahead = 1
			twos = 0
			while cur + ahead < len(dogs):
				if dogs[cur + ahead] == 1:
					break
				else:
					twos += 1
				ahead += 1
			if cur + ahead + 1 >= len(dogs):
				if cur + ahead >= len(dogs):
					score = twos
				else:
					score = twos + (twos + 1) % 2
			elif dogs[cur + ahead + 1] == 1:
				score = twos + (twos + 1) % 2
			else:
				score = twos + (twos + 1) % 2 * 2
			update = cur
			while score > 0:
				multis[update] = score
				update += 2
				score -= 2
		cur += 1
	return multis

def paths_dp(dogs):
	multis = get_multis(dogs)
	twoS = dogs[0] == 2
	twoR = 1 if twoS else 0
	ret = 1
	oneS = dogs[1] == 2
	oneL = 1
	oneR = 0
	if oneS:
		oneR = 1
		if twoS:
			oneR = 2
	ret += 1 + multis[1]
	for (index, dog) in zip(range(2, len(dogs)), dogs[2:]):
		curS = dog == 2
		curL = (oneL + twoR) % LIMIT
		curR = 0
		if curS:
			curR += curL
			if oneS:
				curR += oneL
		val = (curL + multis[index] * oneL) % LIMIT
		ret = ret + val
		twoR = oneR
		oneR = curR
		oneL = curL
		oneS = curS
	return ret % LIMIT
for _ in range(int(input())):
	input()
	dogs = list(map(int, input().split()))
	if len(dogs) < 2:
		ret = len(dogs)
	else:
		ret = paths_dp(dogs)
	print(ret)


test = int(input())
for _ in range(test):
	num = int(input())
	a = [int(i) for i in input().split()]
	if num < 3:
		print(num)
	else:
		b = 2
		c = 1
		d = 0
		e = a[-1] - 1
		f = a[-1] * (a[-2] - 1)
		ans = 0
		q = 0
		for i in range(3, num + 1):
			if a[-i] == 1:
				ans = b + 1
				q = 0
			else:
				if a[1 - i] == 1:
					ans = 1 + b + c
				else:
					ans = 1 + b + c + d
				ans += e
				if e % 2 == 0:
					if e < i - 2:
						ans += 1
					if e < i - 3 and a[e - i + 3] == 2:
						ans += 1
				q = f + 1
			e = f
			f = q
			d = c
			c = b
			b = ans % (10 ** 9 + 7)
		print(b % (10 ** 9 + 7))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dp = [0] * (n + 3)
	dp[0] = 1
	for i in range(1, n):
		dp[i] = dp[i - 1]
		if i - 2 >= 0 and a[i - 2] == 2:
			dp[i] += dp[i - 2]
		if i - 3 >= 0 and a[i - 3] == 2 and (a[i - 2] == 2):
			dp[i] += dp[i - 3]
	dpr = [0] * (n + 3)
	i1 = [0] * n
	i2 = -1
	for i in range(n - 1, -1, -1):
		i1[i] = i2
		if a[i] == 1:
			i2 = i
	for i in range(1, n - 1):
		if a[i - 1] == 1:
			continue
		if i1[i] == -1:
			i2 = n - i - 1
			if i2 % 2:
				z = 2 * (i2 // 2) + 1
			else:
				z = i2
		else:
			i2 = i1[i] - i - 1
			if not i2 % 2:
				z = i2 + 1
				if i1[i] + 1 < n and a[i1[i] + 1] == 2:
					z += 1
			else:
				z = 2 * (i2 // 2) + 1
		dpr[i] = z * dp[i - 1]
	c = 0
	for i in range(n):
		c += dp[i] + dpr[i]
	x = 7 + pow(10, 9)
	print(c % x)


mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dp = [[0] * 4 for _ in range(n)]
	dp[0][0] = 1
	count = [0] * n
	c = 0
	for i in range(n - 1, -1, -1):
		if a[i] == 2:
			c += 1
		else:
			c = 0
		count[i] = c
	for i in range(1, n):
		c = 0
		dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % mod
		if i > 1 and a[i - 2] == 2:
			dp[i][1] = (dp[i - 2][0] + dp[i - 2][1] + dp[i - 2][2]) % mod
		if i < n - 1 and a[i - 1] == 2:
			dp[i][2] = (dp[i - 1][0] + dp[i - 1][1]) % mod
		if i < n - 2 and a[i - 1] == 2 and (a[i + 2] == 2):
			t = (dp[i - 1][0] + dp[i - 1][1]) % mod
			dp[i][3] = t
			if a[i + 1] == 2:
				c = 0
				j = i + 3
				if j < n:
					c = count[j]
					j += c
				if j < n and c % 2 == 0:
					c += 1
					if j < n - 1 and a[j + 1] == 2:
						c += 1
				dp[i][3] = (dp[i][3] + (dp[i - 1][0] + dp[i - 1][1]) * c) % mod
	s = 0
	for i in range(n):
		s = (s + dp[i][0] + dp[i][1] + dp[i][2] + dp[i][3]) % mod
	print(s)


for _ in range(int(input())):
	n = int(input())
	p = [int(o) for o in input().split()]
	s = [0] * n
	pr = [0] * n
	s[n - 1] = 1
	pr[n - 1] = 0
	if n - 2 >= 0:
		s[n - 2] = 2
		pr[n - 2] = 1
	if n - 3 >= 0:
		s[n - 3] = 3
		pr[n - 3] = 1
		if p[n - 3] == 2:
			s[n - 3] += 2
			pr[n - 3] += 1
	i = n - 4
	while i >= 0:
		s[i] = 1
		s[i] += s[i + 1]
		pr[i] = 1
		if p[i] == 2:
			s[i] += s[i + 2]
			s[i] += 1
			pr[i] += 1
			if p[i + 3] == 2:
				s[i] += pr[i + 2]
				pr[i] += pr[i + 2]
			if p[i + 1] == 2:
				s[i] += s[i + 3]
		s[i] = s[i] % 1000000007
		pr[i] = pr[i] % 1000000007
		i -= 1
	print(s[0])


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	if n < 3:
		print(n)
	else:
		a1 = 2
		a2 = 1
		a3 = 0
		c2 = a[-1] - 1
		c1 = a[-1] * (a[-2] - 1)
		tem = 0
		te = 0
		for i in range(3, n + 1):
			if a[-i] == 1:
				tem = a1 + 1
				te = 0
			else:
				if a[1 - i] == 1:
					tem = 1 + a1 + a2
				else:
					tem = 1 + a1 + a2 + a3
				tem += c2
				if c2 % 2 == 0:
					if c2 < i - 2:
						tem += 1
					if c2 < i - 3 and a[c2 - i + 3] == 2:
						tem += 1
				te = c1 + 1
			c2 = c1
			c1 = te
			a3 = a2
			a2 = a1
			a1 = tem % (10 ** 9 + 7)
		print(a1 % (10 ** 9 + 7))


def main():
	for _ in range(int(input())):
		n = int(input())
		dogs = list(map(int, input().split()))
		print(solve(dogs, n))

def compute_muls(dogs, n):
	muls = [0 for _ in range(n)]
	muls[n - 2] = 1 if dogs[n - 3] == 2 else 0
	curr_streak = 0
	if dogs[n - 2] == 2:
		curr_streak += 1
		if dogs[n - 1] == 2:
			curr_streak += 1
	for i in range(n - 3, 0, -1):
		if dogs[i - 1] == 1:
			if dogs[i] == 2:
				curr_streak += 1
			else:
				curr_streak = 0
			continue
		if curr_streak % 2 == 0:
			muls[i] = curr_streak + (1 if i + curr_streak < n - 1 else 0)
			if i + curr_streak < n - 2 and dogs[i + curr_streak + 2] == 2:
				muls[i] += 1
		else:
			muls[i] = curr_streak
		if dogs[i] == 2:
			curr_streak += 1
		else:
			curr_streak = 0
	return muls

def solve(dogs, n):
	if n == 1 or n == 2:
		return n
	if n == 3:
		if dogs[0] == 1:
			return 3
		return 5
	forward_ways = [0 for _ in range(n)]
	forward_ways[0] = 1
	forward_ways[1] = 1
	forward_ways[2] = 2 if dogs[0] == 2 else 1
	for i in range(3, n):
		forward_ways[i] = forward_ways[i - 1]
		if dogs[i - 2] == 2:
			forward_ways[i] += forward_ways[i - 2]
			if dogs[i - 3] == 2:
				forward_ways[i] += forward_ways[i - 3]
		forward_ways[i] %= 1000000007
	total_ways = forward_ways[:]
	muls = compute_muls(dogs, n)
	for i in range(1, n - 1):
		total_ways[i] += forward_ways[i - 1] * muls[i]
	return sum(total_ways) % 1000000007
main()


MOD = 10 ** 9 + 7

def get_input_line():
	return input()

def is_valid(dog, dogs):
	return 0 <= dog < len(dogs)

def clone(my_list):
	return [_ for _ in my_list]

def solve_1(dogs, visited, current_dog, path, paths):
	if not is_valid(current_dog, dogs) or visited[current_dog]:
		return 0
	visited[current_dog] = 1
	path += str(current_dog + 1)
	paths_count = 1
	paths.append(path)
	paths_count += solve_1(dogs, clone(visited), current_dog - 1, path, paths) % MOD
	paths_count += solve_1(dogs, clone(visited), current_dog + 1, path, paths) % MOD
	if dogs[current_dog] == 2:
		paths_count += solve_1(dogs, clone(visited), current_dog - 2, path, paths) % MOD
		paths_count += solve_1(dogs, clone(visited), current_dog + 2, path, paths) % MOD
	return paths_count % MOD

def solve_2(numbers):
	if len(numbers) == 1:
		return 1
	paths = 2
	(a, b, c, d, e, f, g) = (1, 1, 0, 1, 0, 1, 1)
	(new_a, new_b, new_c, new_d, new_e, new_f, new_g) = (0, 0, 0, 0, 0, 0, 0)
	prev2 = numbers[0]
	prev1 = numbers[1]
	for number in numbers[2:]:
		if prev2 == prev1 == number == 2:
			paths += 2 * a + 2 * b - 2 * c - d + e + f - g
			paths = paths % MOD
			new_a = (b + a - c) % MOD
			new_b = (a + b) % MOD
			new_c = (a - c) % MOD
			new_d = b % MOD
			new_e = (a - c + f - d) % MOD
			new_f = (2 * b + e - c - g) % MOD
			new_g = (b + c) % MOD
		if prev2 == prev1 == 2 and number == 1:
			paths += 2 * a + b - c - d + f
			paths = paths % MOD
			new_a = (b + a - c) % MOD
			new_b = (a + b) % MOD
			new_c = (a - c) % MOD
			new_d = b % MOD
			new_e = (a - c + f - d) % MOD
			new_f = b % MOD
			new_g = (b + c) % MOD
		if prev2 == prev1 == 1 and number == 1:
			paths += b
			paths = paths % MOD
			new_a = b % MOD
			new_b = b % MOD
			new_c = 0
			new_d = b % MOD
			new_e = 0
			new_f = b % MOD
			new_g = b % MOD
		if prev2 == prev1 == 1 and number == 2:
			paths += 2 * b - g
			paths = paths % MOD
			new_a = b % MOD
			new_b = b % MOD
			new_c = 0
			new_d = b % MOD
			new_e = 0
			new_f = (2 * b - g) % MOD
			new_g = b % MOD
		if prev2 == 2 and prev1 == 1 and (number == 1):
			paths += 2 * a + b - c
			paths = paths % MOD
			new_a = (b + a - c) % MOD
			new_b = (a + b) % MOD
			new_c = (a - c) % MOD
			new_d = b % MOD
			new_e = (a - c) % MOD
			new_f = b % MOD
			new_g = (b + c) % MOD
		if prev2 == 2 and prev1 == 1 and (number == 2):
			paths += 2 * a + 2 * b - 2 * c + e - g
			paths = paths % MOD
			new_a = (b + a - c) % MOD
			new_b = (a + b) % MOD
			new_c = (a - c) % MOD
			new_d = b % MOD
			new_e = (a - c) % MOD
			new_f = (2 * b + e - c - g) % MOD
			new_g = (b + c) % MOD
		if prev2 == 1 and prev1 == 2 and (number == 1):
			paths += b
			paths = paths % MOD
			new_a = b % MOD
			new_b = b % MOD
			new_c = 0
			new_d = b % MOD
			new_e = 0 % MOD
			new_f = b % MOD
			new_g = b % MOD
		if prev2 == 1 and prev1 == 2 and (number == 2):
			paths += 2 * b - g
			paths = paths % MOD
			new_a = b % MOD
			new_b = b % MOD
			new_c = 0
			new_d = b % MOD
			new_e = 0
			new_f = (2 * b - g) % MOD
			new_g = b % MOD
		(a, b, c, d, e, f, g) = (new_a, new_b, new_c, new_d, new_e, new_f, new_g)
		prev2 = prev1
		prev1 = number
	return paths

def main():
	test_count = int(get_input_line())
	for i in range(test_count):
		_ = int(get_input_line())
		dogs = list(map(int, get_input_line().split()))
		print(solve_2(dogs))
main()


M = 10 ** 9 + 7
t = int(input())
for _ in range(t):
	n = int(input())
	skill = list(map(int, input().split()))
	if n == 1:
		print(1)
	elif n == 2:
		print(2)
	else:
		pair = {n - 3: skill[n - 3], n - 2: 1, n - 1: 0}
		for i in range(n - 4, -1, -1):
			pair[i] = 1
			if skill[i] == 2:
				pair[i] += 1
			if skill[i] == 2 and skill[i + 3] == 2:
				pair[i] += pair[i + 2]
		arr1 = {n - 2: 2, n - 1: 1}
		arr2 = {n - 2: 3, n - 1: 2}
		if skill[-1] == 2:
			arr2[n - 2] += 1
		if skill[-3] == 2:
			arr2[n - 2] += 1
		for i in range(n - 3, -1, -1):
			arr1[i] = 1 + arr1[i + 1] % M
			if skill[i] == 2:
				arr1[i] += arr2[i + 2] % M
			arr2[i] = 2 + arr1[i + 1] % M
			if skill[i] == 2:
				arr2[i] += arr2[i + 2] % M
			if skill[i - 1] == 2:
				arr2[i] += arr1[i + 1] % M
			if skill[i + 1] == 2:
				arr2[i] += pair[i] % M
		print(arr1[0] % M)


def f(x, ud, li):
	if not ud:
		return 1
	m = 1
	if x + 1 in ud:
		ud1 = ud[:]
		ud.remove(x + 1)
		m += f(x + 1, ud, li)
		ud = ud1[:]
	if x - 1 in ud:
		ud1 = ud[:]
		ud.remove(x - 1)
		m += f(x - 1, ud, li)
		ud = ud1[:]
	if li[x] == 2:
		if x + 2 in ud:
			ud1 = ud[:]
			ud.remove(x + 2)
			m += f(x + 2, ud, li)
			ud = ud1[:]
		if x - 2 in ud:
			ud1 = ud[:]
			ud.remove(x - 2)
			m += f(x - 2, ud, li)
			ud = ud1[:]
	return m
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ud = []
	for i in range(1, n):
		ud.append(i)
	c = f(0, ud, a)
	x = 7 + pow(10, 9)
	print(c % x)


import math
import os
import random
import re
import sys
MOD = 10 ** 9 + 7

def check(i, a, used, counts, solution, solutions):
	used[i] = True
	counts[i] += 1
	solution = solution + [i + 1]
	solutions.append(solution)
	dirs = [-1, 1]
	if a[i] > 1:
		dirs.extend([-2, 2])
	for d in dirs:
		j = i + d
		if 0 <= j < len(a) and (not used[j]):
			check(j, a, used, counts, solution, solutions)
	used[i] = False
	solution = solution[:-1]

def solve(a):
	n = len(a)
	l = [[0, 0] for _ in range(n)]
	l[0][0] = 1
	for i in range(1, n):
		l[i][0] = (l[i - 1][0] + ((l[i - 2][0] + l[i - 2][1]) % MOD if i - 2 >= 0 and a[i - 2] > 1 else 0)) % MOD
		l[i][1] = l[i - 1][0] if i + 1 < n and a[i - 1] > 1 else 0
	r = [[0, 0] for _ in range(n)]
	for i in range(n - 2, -1, -1):
		r[i][0] = 1 if a[i + 1] > 1 else 0
		r[i][1] = r[i + 2][0] + r[i + 2][1] + 1 if i + 2 < n and a[i] > 1 and (a[i + 1] > 1) else 0
	res = 0
	d = [0] * n
	d[0] = 1
	for i in range(1, n):
		left = l[i][0]
		left = (l[i][0] + l[i][1]) % MOD
		right = (r[i + 1][0] + r[i + 1][1]) % MOD if a[i - 1] > 1 and i + 1 < n else 0
		d[i] = (left + l[i - 1][0] * right % MOD) % MOD
	res = 0
	for i in range(n):
		res = (res + d[i]) % MOD
	return res
try:
	t = int(input())
except:
	pass
else:
	for _ in range(t):
		solutions = []
		n = int(input())
		a = list(map(int, input().split()))
		print(solve(a))


MOD = 10 ** 9 + 7
import random
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 2:
		print(2)
		continue
	if n == 1:
		print(1)
		continue
	first = [1, 1, 1]
	second = [1, 1, 1]
	if arr[-3] == 2:
		first[1] = 2
		first[2] = 2
	if arr[-1] == 2:
		second[0] = 2
	if arr[-3] == 2:
		second[2] = 2
	onlyfrom2 = 1
	s = (n - 3) % 2
	temp1 = [1, 0, 0]
	temp2 = [0, 1, 0]
	i = n - 4
	while i > 0:
		j = i - 1
		temp1[1] += 1
		temp1[2] += sum(first) + sum(second) * (arr[i] - 1)
		temp1[2] += sum(first) * (arr[j] - 1)
		temp1[1] += (first[0] + first[1] * (arr[i + 2] - 1)) * (arr[j] - 1)
		temp1[2] += (1 + onlyfrom2) * (arr[j] - 1) * (arr[i] - 1)
		onlyfrom2 = sum(first) + sum(second) * (arr[i] - 1)
		temp2[0] += 1
		temp2[2] += sum(first) + sum(second) * (arr[i] - 1)
		temp2[2] += sum(first) * (arr[j] - 1)
		temp2[0] += (first[0] + second[0] * (arr[i] - 1)) * (arr[i + 1] - 1)
		first = [i % MOD for i in temp1]
		second = [i % MOD for i in temp2]
		temp1 = [1, 0, 0]
		temp2 = [0, 1, 0]
		i -= 2
	if s == 1:
		ans = 1 + sum(first) + sum(second) * (arr[0] - 1)
	else:
		ans = sum(first)
	print(ans % MOD)