



As the answer can be very large, return the answer by taking modulo with 1000000007.
If ‘N’=1
So in 1 step we can reach either to ‘X’ , ‘Y’ or ‘Z’ and can not travel back to ‘O’.
Thus there are 0 ways.
If ‘N’ =2
So there are total three ways :
(i) O->X->O
(ii) O->Y->O
(iii) O->Z->O
If ‘N’ = 3
So there are total 6 ways :
(i) O->X->Y->O
(ii) O->X->Z->O
(iii) O->Y->X->O
(iv) O->Y->Z->O
(v) O->Z->X->O
(vi) O->Z->Y->O
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of steps.
For each test case, print a single integer denoting the number of ways.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10000
Where ‘T’ is the total number of test cases, and 'N’ is the number of steps you can make.
Time Limit: 1 sec.
The idea is very simple, as standing at any point we will always have three choices to move. So we will make a recursive function and call it for all three choices and decrease ‘N’ by 1 each time. So when ‘N’ will reach 0 then we will check the current position, if it is ‘O’ then we can say we have found a way.
Our last approach was very simple and easy, but its time complexity was of exponential order. We can improve our solution by taking care of the overlapping subproblems. Thus, we will eliminate the need for solving the same subproblems again and again by storing them in a lookup table. This approach is known as Memoization.
We will initialize a 2-D array say MEMO [4][N+1] with -1, where N is the number of steps. Where MEMO[i][j] equals to -1 means the current state is not explored. Otherwise, MEMO[i][j] will store the number of ways to reach ‘i’ with ‘N’ equal to ‘j’.
As our earlier approach recursion with memoization surely avoided some subproblems but we can still improve time complexity using a bottom-up approach. One observation we can make is that the number of ways to reach the position ‘X’ ={‘O’,’X’,’Y’,’Z’} in the current step is equal to the sum of the number of ways Bob is not at position ‘X’ in the previous steps.