Fibonacci strings are defined as follows:   f1 = «a»  f2 = «b»  fn = fn - 1 fn - 2, n > 2 Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba".You are given a Fibonacci string and m strings si. For each string si, find the number of times it occurs in the given Fibonacci string as a substring.

The first line contains two space-separated integers k and m — the number of a Fibonacci string and the number of queries, correspondingly. Next m lines contain strings si that correspond to the queries. It is guaranteed that strings si aren't empty and consist only of characters "a" and "b". The input limitations for getting 30 points are:    1 ≤ k ≤ 3000  1 ≤ m ≤ 3000  The total length of strings si doesn't exceed 3000  The input limitations for getting 100 points are:    1 ≤ k ≤ 1018  1 ≤ m ≤ 104  The total length of strings si doesn't exceed 105  Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

For each string si print the number of times it occurs in the given Fibonacci string as a substring. Since the numbers can be large enough, print them modulo 1000000007 (109 + 7). Print the answers for the strings in the order in which they are given in the input.

using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApplication4
{
    class Program
    {
        struct Matrix
        {
            public long A1, A2, A3, A4;

            public static Matrix Mul(Matrix m1, Matrix m2)
            {
                Matrix result = new Matrix();
                result.A1 = m1.A1 * m2.A1 + m1.A2 * m2.A3;
                result.A2 = m1.A1 * m2.A2 + m1.A2 * m2.A4;
                result.A3 = m1.A3 * m2.A1 + m1.A4 * m2.A3;
                result.A4 = m1.A3 * m2.A2 + m1.A4 * m2.A4;
                return result;
            }

            public void Mod(long mod)
            {
                A1 %= mod; A2 %= mod; A3 %= mod; A4 %= mod;
            }

            public static Matrix PowerMod(long power, Matrix m, long mod)
            {
                Matrix modLast = m, result = new Matrix();

                while (power != 0)
                {
                    if ((power & 1) == 1)
                    {
                        if (result.A1 == 0)
                            result = modLast;
                        else
                            result = Matrix.Mul(result, modLast);

                        result.Mod(mod);
                    }

                    modLast = Matrix.Mul(modLast, modLast);
                    modLast.Mod(mod);
                    power >>= 1;
                }

                return result;
            }
        }

        static void Main(string[] args)
        {
            string[] fStrings = new string[31];
            fStrings[0] = "a"; fStrings[1] = "b";

            for (int i = 2; i < fStrings.Length; i++)
                fStrings[i] = fStrings[i - 1] + fStrings[i - 2];

            string[] inputStr = Console.ReadLine().Split(' ');
            long m = Convert.ToInt64(inputStr[0]);
            int n = Convert.ToInt32(inputStr[1]);
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                long count = CalCount(s, fStrings, m - 1);
                sw.WriteLine(count % 1000000007);
            }

            sw.Flush();
        }

        static long CalCount(string s, string[] fStrings, long num)
        {
            if (num < 10)
                return ContainCount(s, fStrings[num]);

            Matrix m = new Matrix { A1 = 1, A2 = 1, A3 = 1, A4 = 0 };
            List<int> result = new List<int>();
            int index = 0;

            for (int i = 0; i < fStrings.Length; i++)
            {
                int count = ContainCount(s, fStrings[i]);
                if (count > 1)
                {
                    if(index == 0)
                        index = i;

                    result.Add(count);
                }

                if (fStrings[i].Length > s.Length * 10)
                    break;
            }

            if(result.Count == 0)
                return 0;

            index -= 2;
            Matrix power = Matrix.PowerMod(num - index + 1, m, 1000000007);

            if (result[0] == 2)
            {
                if (result[1] == 3)
                    return power.A2;
                else
                    return power.A1 - 1;
            }
            else
                return power.A1 - (num - index) % 2;
        }

        static int ContainCount(string p, string s)
        {
            if (p.Length > s.Length) return 0;
            int k = -1, count = 0;

            while (true)
            {
                k = s.IndexOf(p, k + 1);
                if (k == -1) break;
                count++;
            }

            return count;
        }
    }
}

using System;
using System.Collections.Generic;

namespace ConsoleApplication4
{
    class Program
    {
        struct Matrix
        {
            public long A1, A2, A3, A4;

            public static Matrix Mul(Matrix m1, Matrix m2)
            {
                Matrix result = new Matrix();
                result.A1 = m1.A1 * m2.A1 + m1.A2 * m2.A3;
                result.A2 = m1.A1 * m2.A2 + m1.A2 * m2.A4;
                result.A3 = m1.A3 * m2.A1 + m1.A4 * m2.A3;
                result.A4 = m1.A3 * m2.A2 + m1.A4 * m2.A4;
                return result;
            }

            public void Mod(long mod)
            {
                A1 %= mod; A2 %= mod; A3 %= mod; A4 %= mod;
            }

            public static Matrix PowerMod(int power, Matrix m, long mod)
            {
                Matrix modLast = m, result = new Matrix();

                while (power != 0)
                {
                    if ((power & 1) == 1)
                    {
                        if (result.A1 == 0)
                            result = modLast;
                        else
                            result = Matrix.Mul(result, modLast);

                        result.Mod(mod);
                    }

                    modLast = Matrix.Mul(modLast, modLast);
                    modLast.Mod(mod);
                    power >>= 1;
                }

                return result;
            }
        }

        static void Main(string[] args)
        {
            string[] fStrings = new string[31];
            fStrings[0] = "a"; fStrings[1] = "b";

            for (int i = 2; i < fStrings.Length; i++)
                fStrings[i] = fStrings[i - 1] + fStrings[i - 2];

            string[] inputStr = Console.ReadLine().Split(' ');
            int m = Convert.ToInt32(inputStr[0]);
            int n = Convert.ToInt32(inputStr[1]);

            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                long count = CalCount(s, fStrings, m - 1);
                Console.WriteLine(count % 1000000007);
            }
        }

        static long CalCount(string s, string[] fStrings, int f)
        {
            if (f < 16)
                return ContainCount(s, fStrings[f]);

            Matrix m = new Matrix { A1 = 1, A2 = 1, A3 = 1, A4 = 0 };
            List<int> result = new List<int>();
            int index = 0;

            for (int i = 0; i < fStrings.Length; i++)
            {
                int count = ContainCount(s, fStrings[i]);
                if (count > 1)
                {
                    if(index == 0)
                        index = i;

                    result.Add(count);
                }

                if (fStrings[i].Length > s.Length * 10)
                    break;
            }

            if(result.Count == 0)
                return 0;

            index--;
            Matrix power = Matrix.PowerMod(f - index + 1, m, 1000000007);

            if (result[0] == 2)
            {
                if (result[1] == 3)
                    return power.A2;
                else
                    return power.A1 - 1;
            }
            else
                return power.A1 - (f - index) % 2;
        }

        static int ContainCount(string p, string s)
        {
            if (p.Length > s.Length) return 0;
            int k = -1, count = 0;

            while (true)
            {
                k = s.IndexOf(p, k + 1);
                if (k == -1) break;
                count++;
            }

            return count;
        }
    }
}

using System;

namespace ConsoleApplication4
{
    class Program
    {
        struct Matrix
        {
            public long A1, A2, A3, A4;

            public static Matrix Mul(Matrix m1, Matrix m2)
            {
                Matrix result = new Matrix();
                result.A1 = m1.A1 * m2.A1 + m1.A2 * m2.A3;
                result.A2 = m1.A1 * m2.A2 + m1.A2 * m2.A4;
                result.A3 = m1.A3 * m2.A1 + m1.A4 * m2.A3;
                result.A4 = m1.A3 * m2.A2 + m1.A4 * m2.A4;
                return result;
            }

            public void Mod(long mod)
            {
                A1 %= mod; A2 %= mod; A3 %= mod; A4 %= mod;
            }

            public static Matrix PowerMod(int power, Matrix m, long mod)
            {
                Matrix modLast = m, result = new Matrix();

                while (power != 0)
                {
                    if ((power & 1) == 1)
                    {
                        if (result.A1 == 0)
                            result = modLast;
                        else
                            result = Matrix.Mul(result, modLast);

                        result.Mod(mod);
                    }

                    modLast = Matrix.Mul(modLast, modLast);
                    modLast.Mod(mod);
                    power >>= 1;
                }

                return result;
            }
        }

        static void Main(string[] args)
        {
            string[] fStrings = new string[31];
            fStrings[0] = "a"; fStrings[1] = "b";

            for (int i = 2; i < fStrings.Length; i++)
                fStrings[i] = fStrings[i - 1] + fStrings[i - 2];

            string[] inputStr = Console.ReadLine().Split(' ');
            int m = Convert.ToInt32(inputStr[0]);
            int n = Convert.ToInt32(inputStr[1]);

            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                long count = CalCount(s, fStrings, m - 1);
                Console.WriteLine(count % 1000000007);
            }
        }

        static long CalCount(string s, string[] fStrings, int f)
        {
            if (f < 16)
                return ContainCount(s, fStrings[f]);

            Matrix m = new Matrix { A1 = 1, A2 = 1, A3 = 1, A4 = 0 };
            int[] result = new int[fStrings.Length];

            for (int i = 0; i < fStrings.Length; i++)
            {
                if (fStrings[i].Length > s.Length)
                    result[i] = ContainCount(s, fStrings[i]);

                if (fStrings[i].Length > s.Length * 10)
                    break;
            }

            int index = -1;

            for (int i = 0; i < result.Length; i++)
            {
                if (result[i] > 1)
                {
                    index = i;
                    break;
                }
            }

            if (index < 0)
                return 0;

            index--;
            Matrix power = Matrix.PowerMod(f - index + 1, m, 1000000007);

            if (result[index + 1] == 2)
            {
                if (result[index + 2] == 3)
                    return power.A2;
                else
                    return power.A1 - 1;
            }
            else
                return power.A1 - (f - index) % 2;
        }

        static int ContainCount(string p, string s)
        {
            int k = -1;
            int count = 0;

            while (true)
            {
                k = s.IndexOf(p, k + 1);
                if (k == -1) break;
                count++;
            }

            return count;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        struct Matrix
        {
            public Int64 A1, A2, A3, A4;

            public static Matrix Mul(Matrix m1, Matrix m2)
            {
                Matrix result = new Matrix();
                result.A1 = m1.A1 * m2.A1 + m1.A2 * m2.A3;
                result.A2 = m1.A1 * m2.A2 + m1.A2 * m2.A4;
                result.A3 = m1.A3 * m2.A1 + m1.A4 * m2.A3;
                result.A4 = m1.A3 * m2.A2 + m1.A4 * m2.A4;
                return result;
            }

            public void Mod(Int64 mod)
            {
                A1 %= mod; A2 %= mod; A3 %= mod; A4 %= mod;
            }

            public static Matrix PowerMod(int power, Matrix m, Int64 mod)
            {
                Matrix modLast = m, result = new Matrix();

                while (power != 0)
                {
                    if ((power & 1) == 1)
                    {
                        if (result.A1 == 0)
                            result = modLast;
                        else
                            result = Matrix.Mul(result, modLast);

                        result.Mod(mod);
                    }

                    modLast = Matrix.Mul(modLast, modLast);
                    modLast.Mod(mod);
                    power >>= 1;
                }

                return result;
            }
        }

        static void Main(string[] args)
        {
            string[] fStrings = new string[31];
            fStrings[0] = "a"; fStrings[1] = "b";

            for (int i = 2; i < fStrings.Length; i++)
                fStrings[i] = fStrings[i - 1] + fStrings[i - 2];

            string[] inputStr = Console.ReadLine().Split(' ');
            int m = Convert.ToInt32(inputStr[0]);
            int n = Convert.ToInt32(inputStr[1]);

            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                Int64 count = CalCount(s, fStrings, m - 1);
                Console.WriteLine(count % 1000000007);
            }
        }

        static Int64 CalCount(string s, string[] fStrings, int f)
        {
            if (f < fStrings.Length)
                return ContainCount(s, fStrings[f]);

            Matrix m = new Matrix { A1 = 1, A2 = 1, A3 = 1, A4 = 0 };
            int[] result = new int[fStrings.Length];

            for (int i = 0; i < fStrings.Length; i++)
            {
                if (fStrings[i].Length > s.Length)
                    result[i] = ContainCount(s, fStrings[i]);

                if (fStrings[i].Length > s.Length * 10)
                    break;
            }

            int index = Array.LastIndexOf(result, 1);

            Matrix power = Matrix.PowerMod(f - index + 1, m, 1000000007);

            if (index < 0)
                return -1;
            else if (result[index + 1] == 2)
            {
                if (result[index + 2] == 3)
                    return power.A2;
                else
                    return power.A2 - 1;
            }
            else
                return power.A1 - (f - index) % 2;
        }

        static int ContainCount(string p, string s)
        {
            int k = -1;
            int count = 0;

            while (true)
            {
                k = s.IndexOf(p, k + 1);
                if (k == -1) break;
                count++;
            }

            return count;
        }
    }
}

using System;

namespace ConsoleApplication4
{
    class Program
    {
        struct Matrix
        {
            public long A1, A2, A3, A4;

            public static Matrix Mul(Matrix m1, Matrix m2)
            {
                Matrix result = new Matrix();
                result.A1 = m1.A1 * m2.A1 + m1.A2 * m2.A3;
                result.A2 = m1.A1 * m2.A2 + m1.A2 * m2.A4;
                result.A3 = m1.A3 * m2.A1 + m1.A4 * m2.A3;
                result.A4 = m1.A3 * m2.A2 + m1.A4 * m2.A4;
                return result;
            }

            public void Mod(long mod)
            {
                A1 %= mod; A2 %= mod; A3 %= mod; A4 %= mod;
            }

            public static Matrix PowerMod(int power, Matrix m, long mod)
            {
                Matrix modLast = m, result = new Matrix();

                while (power != 0)
                {
                    if ((power & 1) == 1)
                    {
                        if (result.A1 == 0)
                            result = modLast;
                        else
                            result = Matrix.Mul(result, modLast);

                        result.Mod(mod);
                    }

                    modLast = Matrix.Mul(modLast, modLast);
                    modLast.Mod(mod);
                    power >>= 1;
                }

                return result;
            }
        }

        static void Main(string[] args)
        {
            string[] fStrings = new string[31];
            fStrings[0] = "a"; fStrings[1] = "b";

            for (int i = 2; i < fStrings.Length; i++)
                fStrings[i] = fStrings[i - 1] + fStrings[i - 2];

            string[] inputStr = Console.ReadLine().Split(' ');
            int m = Convert.ToInt32(inputStr[0]);
            int n = Convert.ToInt32(inputStr[1]);

            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                long count = CalCount(s, fStrings, m - 1);
                Console.WriteLine(count % 1000000007);
            }
        }

        static long CalCount(string s, string[] fStrings, int f)
        {
            if (f < 20)
                return ContainCount(s, fStrings[f]);

            Matrix m = new Matrix { A1 = 1, A2 = 1, A3 = 1, A4 = 0 };
            int[] result = new int[fStrings.Length];

            for (int i = 0; i < fStrings.Length; i++)
            {
                if (fStrings[i].Length > s.Length)
                    result[i] = ContainCount(s, fStrings[i]);

                if (fStrings[i].Length > s.Length * 10)
                    break;
            }

            int index = Array.LastIndexOf(result, 1);

            Matrix power = Matrix.PowerMod(f - index + 1, m, 1000000007);

            if (index < 0)
                return 0;
            else if (result[index + 1] == 2)
            {
                if (result[index + 2] == 3)
                    return power.A2;
                else
                    return power.A1 - 1;
            }
            else
                return power.A1 - (f - index) % 2;
        }

        static int ContainCount(string p, string s)
        {
            int k = -1;
            int count = 0;

            while (true)
            {
                k = s.IndexOf(p, k + 1);
                if (k == -1) break;
                count++;
            }

            return count;
        }
    }
}

using System;

namespace ConsoleApplication4
{
    class Program
    {
        struct Matrix
        {
            public long A1, A2, A3, A4;

            public static Matrix Mul(Matrix m1, Matrix m2)
            {
                Matrix result = new Matrix();
                result.A1 = m1.A1 * m2.A1 + m1.A2 * m2.A3;
                result.A2 = m1.A1 * m2.A2 + m1.A2 * m2.A4;
                result.A3 = m1.A3 * m2.A1 + m1.A4 * m2.A3;
                result.A4 = m1.A3 * m2.A2 + m1.A4 * m2.A4;
                return result;
            }

            public void Mod(long mod)
            {
                A1 %= mod; A2 %= mod; A3 %= mod; A4 %= mod;
            }

            public static Matrix PowerMod(int power, Matrix m, long mod)
            {
                Matrix modLast = m, result = new Matrix();

                while (power != 0)
                {
                    if ((power & 1) == 1)
                    {
                        if (result.A1 == 0)
                            result = modLast;
                        else
                            result = Matrix.Mul(result, modLast);

                        result.Mod(mod);
                    }

                    modLast = Matrix.Mul(modLast, modLast);
                    modLast.Mod(mod);
                    power >>= 1;
                }

                return result;
            }
        }

        static void Main(string[] args)
        {
            string[] fStrings = new string[31];
            fStrings[0] = "a"; fStrings[1] = "b";

            for (int i = 2; i < fStrings.Length; i++)
                fStrings[i] = fStrings[i - 1] + fStrings[i - 2];

            string[] inputStr = Console.ReadLine().Split(' ');
            int m = Convert.ToInt32(inputStr[0]);
            int n = Convert.ToInt32(inputStr[1]);

            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                long count = CalCount(s, fStrings, m - 1);
                Console.WriteLine(count % 1000000007);
            }
        }

        static long CalCount(string s, string[] fStrings, int f)
        {
            if (f < 16)
                return ContainCount(s, fStrings[f]);

            Matrix m = new Matrix { A1 = 1, A2 = 1, A3 = 1, A4 = 0 };
            int[] result = new int[fStrings.Length];

            for (int i = 0; i < fStrings.Length; i++)
            {
                if (fStrings[i].Length > s.Length)
                    result[i] = ContainCount(s, fStrings[i]);

                if (fStrings[i].Length > s.Length * 10)
                    break;
            }

            int index = Array.IndexOf(result, 1);
            if (index < 0)
                return 0;

            if (result[index + 1] == 1)
                index++;

            Matrix power = Matrix.PowerMod(f - index + 1, m, 1000000007);

            if (result[index + 1] == 2)
            {
                if (result[index + 2] == 3)
                    return power.A2;
                else
                    return power.A1 - 1;
            }
            else
                return power.A1 - (f - index) % 2;
        }

        static int ContainCount(string p, string s)
        {
            int k = -1;
            int count = 0;

            while (true)
            {
                k = s.IndexOf(p, k + 1);
                if (k == -1) break;
                count++;
            }

            return count;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        struct Matrix
        {
            public long A1, A2, A3, A4;

            public static Matrix Mul(Matrix m1, Matrix m2)
            {
                Matrix result = new Matrix();
                result.A1 = m1.A1 * m2.A1 + m1.A2 * m2.A3;
                result.A2 = m1.A1 * m2.A2 + m1.A2 * m2.A4;
                result.A3 = m1.A3 * m2.A1 + m1.A4 * m2.A3;
                result.A4 = m1.A3 * m2.A2 + m1.A4 * m2.A4;
                return result;
            }

            public void Mod(long mod)
            {
                A1 %= mod; A2 %= mod; A3 %= mod; A4 %= mod;
            }

            public static Matrix PowerMod(int power, Matrix m, long mod)
            {
                Matrix modLast = m, result = new Matrix();

                while (power != 0)
                {
                    if ((power & 1) == 1)
                    {
                        if (result.A1 == 0)
                            result = modLast;
                        else
                            result = Matrix.Mul(result, modLast);

                        result.Mod(mod);
                    }

                    modLast = Matrix.Mul(modLast, modLast);
                    modLast.Mod(mod);
                    power >>= 1;
                }

                return result;
            }
        }

        static void Main(string[] args)
        {
            string[] fStrings = new string[31];
            fStrings[0] = "a"; fStrings[1] = "b";

            for (int i = 2; i < fStrings.Length; i++)
                fStrings[i] = fStrings[i - 1] + fStrings[i - 2];

            string[] inputStr = Console.ReadLine().Split(' ');
            int m = Convert.ToInt32(inputStr[0]);
            int n = Convert.ToInt32(inputStr[1]);

            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                long count = CalCount(s, fStrings, m - 1);
                Console.WriteLine(count % 1000000007);
            }
        }

        static long CalCount(string s, string[] fStrings, int f)
        {
            if (f < fStrings.Length)
                return ContainCount(s, fStrings[f]);

            Matrix m = new Matrix { A1 = 1, A2 = 1, A3 = 1, A4 = 0 };
            int[] result = new int[fStrings.Length];

            for (int i = 0; i < fStrings.Length; i++)
            {
                if (fStrings[i].Length > s.Length)
                    result[i] = ContainCount(s, fStrings[i]);

                if (fStrings[i].Length > s.Length * 10)
                    break;
            }

            int index = Array.LastIndexOf(result, 1);

            Matrix power = Matrix.PowerMod(f - index + 1, m, 1000000007);

            if (index < 0)
                return -1;
            else if (result[index + 1] == 2)
            {
                if (result[index + 2] == 3)
                    return power.A2;
                else
                    return power.A2 - 1;
            }
            else
                return power.A1 - (f - index) % 2;
        }

        static int ContainCount(string p, string s)
        {
            int k = -1;
            int count = 0;

            while (true)
            {
                k = s.IndexOf(p, k + 1);
                if (k == -1) break;
                count++;
            }

            return count;
        }
    }
}