Towers
Difficulty: Hard

One day John had to take care of his little nephew Jim. He was very busy, so he gave Jim a big bag full of building bricks. The bricks are of various heights: at most 15 different heights. For each height, the bag contains infinitely many bricks.   
<br>
Now, Jim’s task is to build every possible tower of height $N$ from the given bricks. Bricks are stacked vertically only and stand in an upright position. Two towers are different if their brick height sequences are different.
<br>
Jim is good at building towers and can build one tower in exactly 2 minutes, regardless of its height. John wants to know the time Jim requires to build all possible towers.  

**Input Format**  
There are $3$ lines of input. First line contains an integer, $N$, the height of tower to be built. Second line contains another integer, $K$, which represents different heights of bricks available in the bag. Third line contains $K$ distinct integers representing the available heights.  


**Output Format**  
In one line print the number of minutes Jim requires to build all possible towers. As this number can be very large, print the number modulo $(10^9+7)$.  


**Constraints**  
$1 \le N \le 10^{18}$  
$1 \le K \le 15$  
All heights will be unique.  
Height of each brick will lie in range [1, 15].  

**Sample Input#00**

    10
    1
    1

**Sample Output#00**  

    2

**Explanation#00:**  There is exactly one type of brick, so there is exactly one tower of height 10. Building any tower takes 2 minutes, so the answer is 2.

**Sample Input#01**  

    5
    2
    2 3

**Sample Output#01**  

    4

**Explanation #01:** There are two types of bricks. There are two different towers of height 5 which can be build from these types of bricks: $[2,3]$ and $[3,2]$. They are different, because the sequence of bricks in them is different. Building any tower takes 2 minutes, so the answer is $2 \times 2 = 4$.

**Sample Input#03**  

    19
    2
    4 5

**Sample Output#03**

    8

**Explanation #03:** There are two types of bricks. Jim can build 4 different towers of height $19$ from these bricks: $[5,5,5,4]$, $[5,5,4,5]$, $[5,4,5,5]$ and $[4,5,5,5]$. Building any tower takes 2 minutes, so the answer is $2 \times 4 = 8$.


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:
#
# Complete the 'solve' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. LONG_INTEGER n
#  2. INTEGER_ARRAY heights
#

def solve(n, heights):
    # Write your code here




Reference solution (C++):
###C++  
```cpp  
--C++
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))

typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<ull> > matrix;
const int MOD = 1000000000 + 7;

matrix mmul(matrix M1, matrix M2)
{
   matrix R(15);
   FOR(i, 15)
   {
      R[i].assign(15, 0);
      FOR(j, 15)
      {       
         FOR(k, 15)
         {
            R[i][j] = (R[i][j] + M1[i][k] * M2[k][j]) % MOD;
         }
      }
   }
   return R;
}

matrix mpow(matrix M, ull n)
{
   if(n == 0)
   {
      matrix M1(15);
      FOR(i, 15)
      {
         M1[i].assign(15, 0);
         FOR(j, 15)
         {
            if(i == j)
               M1[i][j] = 1;
         }
      }
      return M1;
   }
   else if(n % 2 == 0)
   {
      matrix R = mpow(M, n / 2);
      return mmul(R, R);
   }
   else
   {
      matrix R = mpow(M, n - 1);
      return mmul(R, M);
   }
}

ull V[15];
int main()
{
//GENERATE M
   matrix M(15);
   FOR(i, 15)
   {
      M[i].assign(15, 0);
      FOR(j, 15)
      {
         if(i + 1 == j)
         {
            M[i][j] = 1;
         }
      }
   }
//INPUT
   int k;
   ull n;
   vi jumps;
   vi res;
   ull tmp;

   cin >> n;
   cin >> k;
   FOR(i, 15)
   {
      V[i] = 0;
   }      
   M[14].assign(15, 0);
   FOR(i, k)
   {
      cin >> tmp;
      M[14][14 - tmp + 1] = 1;  
   }      
   FOR(i, 15)
   {
      FOR(j, i + 1)
      {
         if(M[14][14 - j] == 1) 
         {
            if(i - j == 0)
            {
               V[i]++;
            }
            else 
            {
               V[i] += V[i - j - 1];
            }
         }
      }
   }
   if(n <= 15)
   {
      ull res = 0;
      res = (V[n - 1] * 2) % MOD;
      cout << res << endl;
   }
   else
   {
      matrix R = mpow(M, n - 15);
      ull res = 0;
      FOR(j, 15)
      {
         res = (res + R[14][j] * V[j]) % MOD;
      }
      res = (res * 2) % MOD;
      cout << res << endl;   
   }
   return 0;
}
```