Girlfriend & Necklace
Difficulty: Hard

Mr. X wants to buy a necklace for his girlfriend.
The available necklaces are bi-colored (red and blue beads).

Mr. X desperately wants to impress his girlfriend and knows that she will like the necklace only if **every prime length continuous sub-sequence of beads in the necklace** has more or equal number of red beads than blue beads.

Given the number of beads in the necklace N, Mr. X wants to know the number of all possible such necklaces.

_Note: - It is given that the necklace is a single string and not a loop._


**Input Format**  
The first line of the input contains an integer *T*, the number of testcases.  
*T* lines follow, each line containing *N*, the number of beads in the necklace.  

**Constraints**  
1 &le; T &le; 10<sup>4</sup>  
2 &le; N &le; 10<sup>18</sup>  

**Output Format**  
For each testcase, print in a newline, the number of such necklaces that are possible.  If the answer is greater than or equal to 10<sup>9</sup>+7, print the answer modulo ( % ) 10<sup>9</sup>+7.

**Sample Input**

    2
    2
    3

**Sample Output**

    3
    4

**Explanation**

For the first testcase, valid arrangement of beads are 

    BR RB RR

For the second testcase, valid arrangement of beads are

    BRR RBR RRR RRB

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
###C++  
```cpp  
#include<bits/stdc++.h>

using namespace std;

#define MOD 1000000007

class Matrix {
    public:
        int **mat;
        int row;
        int col;

        Matrix()
        {
        }

        Matrix(int row = 2, int col = 2)
        {
            this->row = row;
            this->col = col;
            mat = new int*[row];
            for(int i = 0; i < row; i++)
                mat[i] = new int[col];
        }

        Matrix(int **n, int row, int col, int mod)
        {
            this->row = row;
            this->col = col;
            mat = new int*[row];
            for(int i = 0; i < row; i++)
            {
                mat[i] = new int[col];
                for(int j = 0; j < col; j++)
                {
                    mat[i][j] = n[i][j];
                }
            }
        }

        Matrix operator * (const Matrix &b)
        {
            Matrix *c = new Matrix(this->row, b.col);
            assert(this->col == b.row);
            // This might be wrong. Please be careful
            for(int i = 0; i < this->row; i++)
            {
                for(int j = 0; j < this->col; j++)
                {
                    c->mat[i][j] = 0;
                    for(int k = 0; k < this->col; k++)
                    {
                        long long val = (long long)mat[i][k] * (long long)b.mat[k][j];
                        assert(val >= 0);
                        val = val % MOD;
                        assert(val >= 0);
                        c->mat[i][j] = (c->mat[i][j] + val) % MOD;
                        assert(c->mat[i][j] >= 0);
                    }
                }
            }
            return *c;
        }

        void print_matrix()
        {
            for(int i = 0; i < row; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    cout << mat[i][j] << " ";
                }
                cout << endl;
            }
        }
};

Matrix raised_to_pow(Matrix a, long long n)
{
    if(n == 1)
        return a;

    Matrix b = raised_to_pow(a, n/2);
    Matrix c = b * b;
    if(n&1)
        c = c * a;

    return c;
}

int main()
{
    int **a = new int*[3];
    a[0] = new int[3];
    a[1] = new int[3];
    a[2] = new int[3];
    a[0][0] = a[0][1] = a[1][2] = a[2][0] = 1;
    a[0][2] = a[1][0] = a[1][1] = a[2][1] = a[2][2] = 0;

    int **b = new int*[1];
    b[0] = new int[3];
    b[0][0] = 3;
    b[0][1] = 2;
    b[0][2] = 1;

    Matrix *row_mat = new Matrix(b, 1, 3, MOD);
    Matrix *x = new Matrix(a, 3, 3, MOD);

    int t;
    long long n;
    cin >> t;
    assert(1 <= t and t <= 1e4);
    while(t--)
    {
        cin >> n;
        assert(2 <= n and n <= 1e18);
        if(n == 1)
            cout << 1 << endl;
        else if(n == 2)
            cout << 3 << endl;
        else if(n == 3)
            cout << 4 << endl;
        else if(n == 4)
            cout << 6 << endl;
        else if(n == 0)
            cout << 0 << endl;
        else
        {
            Matrix c = raised_to_pow(*x, n - 4);
            Matrix answer = (*row_mat) * c;
            long long x = 0;
            for(int i = 0; i < 3; i++)
            {
                x += answer.mat[0][i];
                if(x >= MOD)
                    x -= MOD;
            }
           
            cout << x << endl;
        }
    }
    return 0;
}
```