Read problems statements in English, Mandarin Chinese and Russian as well. 

Richard Winters: We're not lost, Private... we're in Normandy.

In WW2, allies have placed their mines on the ground  which can be considered as a gird of size N*M. Rows are numbered by integers from 1 to N and columns are numbered by integers from 1 to M. Each cell (i,j)  has a mine in it if (i+j) is divisible by 2. All the other cells are empty.

Two cells are neighbors if they share a boundary or a corner. Your aim is to move from the first row to the last row using some special rules that the automatic mine placing system follows. The mines won't blow if the following rules are followed:

If your current row number is odd, from a 'mined' cell(ie. cell having a mine in it) you can move only to a neighboring 'mined' cells in the next row, and similarly, from a 'not mined'(ie. an empty cell) cell you can only move to a neighboring 'not mined' cell in the next row.

If your current row number is even, you can move to any neighboring cell in the next row, irrespective of the mining.

You have to find the number of ways to reach row number N starting from row number 1, modulo 10^{9}+7. Two ways are different, if there is at least one different cell on the paths.

------ Input ------ 

First line contains T, the number of testcases. Each testcase consists of N and M in one line.

------ Output ------ 

For each testcase, print the required answer in one line.

------ Constraints ------ 

1 ≤ T ≤ 10
1 ≤ N ≤ 10^{9}
1 ≤ M ≤ 30

------ Example ------ 

Input:
2
1 5
2 5

Output:
5
8

------ Explanation ------ 

Example case 1.You can start at any of the 5 cells available.

Example case 2.

8 ways are:

(1,1) to (2,2)

(1,2) to (2,1)

(1,2) to (2,3)

(1,3) to (2,2)

(1,3) to (2,4)

(1,4) to (2,3)

(1,4) to (2,5)

(1,5) to (2,4)

Source: codechef | Difficulty: VERY_HARD
Tags: Matrices

Dynamic programming

Mathematics


Solutions:
mod = 1000000007

def mmul(A, B):
	s = len(A)
	C = [[0 for i in range(s)] for j in range(s)]
	for i in range(s):
		for j in range(s):
			for k in range(s):
				C[i][j] += A[i][k] * B[k][j]
				C[i][j] %= mod
	return C

def mpow(A, N, I):
	if N == 0:
		return I
	if N == 1:
		return A
	ret = mpow(A, N // 2, I)
	ret1 = mmul(ret, ret)
	if N % 2 == 0:
		return ret1
	else:
		return mmul(ret1, A)
test = int(input())
for _ in range(test):
	(N, n) = [int(c) for c in input().split()]
	I = [[0 for i in range(n)] for j in range(n)]
	for i in range(0, n):
		I[i][i] = 1
	odd = [[0 for i in range(n)] for j in range(n)]
	even = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		odd[i][i] = 1
		if i >= 1:
			even[i][i - 1] = 1
			odd[i][i - 1] = 1
		if i < n - 1:
			even[i][i + 1] = 1
			odd[i][i + 1] = 1
	T = mmul(even, odd)
	N -= 1
	ans = mpow(T, N // 2, I)
	ret = 0
	if N % 2:
		ans = mmul(ans, even)
	for i in ans:
		for j in i:
			ret += j
			ret %= mod
	print(ret)