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.

{$R+}
const md = 1000000007;
var
uu,ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
anss,a1,a2,a3,a4,c1,t1,t2:array[0..100000]of int64;
w,s:ansistring;
k:int64;
pp:array[0..2000000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var ha,i,j,k:longint;
cc:int64;
 begin
  if c<=0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;
  for i:=1 to uu do
    if (p1=a1[i])and(p2=a2[i])and(p3=a3[i])and(p4=a4[i])
    and(c=c1[i])and(r1=t1[i])and(r2=t2[i]) then
      begin
       find:=anss[i];
                    exit;
      end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then
    begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,2];
     find:=x[1,2]
    end  else
       begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,1];
     find:=x[1,1];
    end;
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  for i:=length(s)+1 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
     if tt=4 then break;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 38 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     for i:=2 to length(s) do
       begin
     pp[i]:=pp[i-1];
     while (s[pp[i]+1]<>s[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if s[pp[i]+1]=s[i] then inc(pp[i]);

       end;
     p:=0;
     for j:=1 to 36 do
       begin
        if length(f[j])>length(s)*5 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=p;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p2+p3+1,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

const fin='';
    fout='';
var f:array [0..40] of ansistring;
    fs,fe:array [0..4000] of ansistring;
    fvh:array [0..4000] of int64;
    ls,n,i,ii,k:longint; s:ansistring;
    fi,fo:text;
procedure wr (x:int64);
  begin
    writeln (fo,x);
  end;
function vh (s,ss:ansistring):int64;
var i,j,f:longint;ans:int64;
  begin
    ans:=0;
    for i:=1 to length(ss)-length(s)+1 do
      begin
        f:=0;
        for j:=1 to length(s) do
          if ss[i+j-1]<>s[j] then
            begin
                f:=1;break;
            end;
        if f=0 then
            ans:=(ans+1) mod 1000000007;
      end;
    vh:=ans mod 1000000007 ;
  end;
procedure xuli;
begin
    while not eof(fi) do
     begin
     f[1]:='a'; f[2]:='b';
     readln (fi,s);
     for i:=3 to 20 do f[i]:=f[i-1]+f[i-2];
     if n<=20 then
        begin
            wr(vh(s,f[n]));continue;
        end;
     ls:=length(s);
     fs[19]:=copy(f[19],1,ls-1);
     fs[20]:=copy(f[20],1,ls-1);
     fe[19]:=copy(f[19],length(f[19])-ls+2,ls-1);
     fe[20]:=copy(f[20],length(f[20])-ls+2,ls-1);
     fvh[19]:=vh(s,f[19]);
     fvh[20]:=vh(s,f[20]);
     for i:=21 to n do
       begin
          fs[i]:=fs[i-1];
          fe[i]:=fe[i-2];
          fvh[i]:=(fvh[i-1]+fvh[i-2]+vh(s,fe[i-1]+fs[i-2])) mod 1000000007;
       end;
     wr(fvh[n]);
     end;
end;
begin
     assign (fi,fin); reset(fi);
     assign (fo,fout); rewrite(fo);
     readln (fi,n,k);
     xuli;
     close(fi); close(fo);
end.

const base=1000000000+7;
var  f,g:text;
     a,b,ba,ab,bb:array[1..3000]of int64;
     n:longint;
     xau:array[1..2]of ansistring;
     s:ansistring;
     mag:array[1..100000]of longint;

function tinh(var p:ansistring; x,y:longint):int64;
var i,q:longint;
    tinhz:int64;
begin
  q:=0;
  tinhz:=0;
  for i:=1 to length(p) do begin
    while (q>0)and(p[i]<>s[q+1])do q:=mag[q];
    if p[i]=s[q+1] then inc(q);
    if q=length(s) then begin
      q:=mag[q];
      if (i>x)and((i-length(s)+1)<=y) then inc(tinhz);
    end;
  end;
  tinhz:=tinhz mod base;
  tinh:=tinhz;
end;

procedure kmp;
var i,q:longint;
begin
  q:=0;
  mag[1]:=0;
  for i:=2 to length(s)do begin
    while (q>0)and(s[q+1]<>s[i])do q:=mag[q];
    if s[q+1]=s[i]then inc(q);
    mag[i]:=q;
  end;
end;

procedure solve;
var temp,first,second,count:longint;
    xauab,xauba,xaubb:ansistring;
    timeab,timeba,timebb,tong:int64;
begin
  count:=1;
  xau[1]:='a';
  xau[2]:='b';
  first:=1; second:=2;
  while (length(xau[first]))<length(s) do begin
    xau[first]:=xau[second]+xau[first];
    temp:=first;
    first:=second;
    second:=temp;
    inc(count);
  end;
  tong:=0;
  if count>n then tong:=0
  else begin
    xauab:=xau[first]+xau[second];
    timeab:=tinh(xauab,length(xau[first]),length(xau[first]));
    xauba:=xau[second]+xau[first];
    timeba:=tinh(xauba,length(xau[second]),length(xau[second]));
    xaubb:=xau[second]+xau[second];
    timebb:=tinh(xaubb,length(xau[second]),length(xau[second]));
    tong:=(tong+timeab*(ab[n-count+1]))mod base;
    tong:=(tong+timeba*(ba[n-count+1]))mod base;
    tong:=(tong+timebb*(bb[n-count+1]))mod base;
    tong:=(tong+tinh(xau[first],0,length(xau[first]))*a[n-count+1])mod base;
    tong:=(tong+tinh(xau[second],0,length(xau[second]))*b[n-count+1])mod base;
  end;
  writeln(g,tong);
end;

procedure nhap;
var space:char;
    test,i:longint;
begin
  assign(f,'');
  assign(g,'');
  reset(f);
  rewrite(g);
  readln(f,n,test);
  for i:=1 to test do begin
    readln(f,s);
    kmp;
    solve;
  end;
  close(f);
  close(g);
end;

procedure tinhtruoc;
var i:longint;
begin
  a[1]:=1; a[2]:=0; a[3]:=1;
  for i:=4 to 3000 do a[i]:=(a[i-1]+a[i-2])mod base;
  b[1]:=0; b[2]:=1;
  for i:=3 to 3000 do b[i]:=(b[i-1]+b[i-2])mod base;
  ba[1]:=0; ba[2]:=0; ba[3]:=1;
  for i:=4 to 3000 do ba[i]:=(ba[i-1]+ba[i-2])mod base;
  ab[1]:=0; ab[2]:=0; ab[3]:=0; ab[4]:=1;
  for i:=5 to 3000 do begin
    ab[i]:=ab[i-1]+ab[i-2];
    if (i mod 2)=0 then inc(ab[i]);
    ab[i]:=ab[i]mod base;
  end;
  bb[1]:=0; bb[2]:=0; bb[3]:=0; bb[4]:=0;
  for i:=5 to 3000 do begin
    bb[i]:=bb[i-1]+bb[i-2];
    if (i mod 2)=1 then inc(bb[i]);
    bb[i]:=bb[i]mod base;
  end;
end;

begin
  tinhtruoc;
  nhap;
end.

var f:array [0..40] of ansistring;
    fs,fe:array [0..4000] of ansistring;
    fvh:array [0..4000] of int64;
    ls,n,i,ii,k:longint; s:ansistring;
procedure wr (x:int64);
  begin
    writeln (x);
  end;
function vh (s,ss:ansistring):int64;
var i,j,f:longint;ans:int64;
  begin
    ans:=0;
    for i:=1 to length(ss)-length(s)+1 do
      begin
        f:=0;
        for j:=1 to length(s) do
          if ss[i+j-1]<>s[j] then begin f:=1;break;end;
        if f=0 then ans:=(ans+1) mod 1000000007;
      end;
    vh:=ans mod 1000000007;
  end;

begin             {
assign (input,'input.txt');
assign (output,'output.txt');
reset (input);
rewrite (output);  }
readln (n,k);
for ii:=1 to k do
begin
readln (s);
f[1]:='a';
f[2]:='b';
for i:=3 to 20 do f[i]:=f[i-1]+f[i-2];
if n<=20 then begin wr(vh(s,f[n]));continue;end;
ls:=length(s);
fs[19]:=copy(f[19],1,ls-1);
fs[20]:=copy(f[20],1,ls-1);
fe[19]:=copy(f[19],length(f[19])-ls+2,ls-1);
fe[20]:=copy(f[20],length(f[20])-ls+2,ls-1);
fvh[19]:=vh(s,f[19]);
fvh[20]:=vh(s,f[20]);
//writeln (length(f[20]));
for i:=21 to n do
  begin
     fs[i]:=fs[i-1];
     fe[i]:=fe[i-2];
     fvh[i]:=(fvh[i-1]+fvh[i-2]+vh(s,fe[i-1]+fs[i-2])) mod 1000000007;
  end;
wr(fvh[n]);
end;
end.

uses math;
const md=1000000007;
Var c:array[0..3003]of longint;
    f:array[0..3003]of ansistring;
    p:array[0..10333]of longint;
    s1,sss,s2,s,ss:ansistring;
    i,j,u,n,m,l:longint;
 function count(var s,ss:ansistring):longint;
 var i,j,res:longint;
   begin
     sss:=s+'$'+ss;
     p[1]:=0;
     res:=0;
     for i:=2 to length(sss) do
       begin
         j:=p[i-1];
         while (j>0)and(sss[j+1]<>sss[i]) do j:=p[j];
         if sss[j+1]=sss[i] then j:=j+1;
         p[i]:=j;
         if p[i]=length(s) then res:=res+1;
       end;
     count:=res;
   end;
  begin
{    assign(input,'input.txt');
    reset(input);    }
    readln(n,m);
    f[1]:='a';
    f[2]:='b';
    for i:=3 to n do
      begin
        if length(f[i-1])>3000 then s1:=copy(f[i-1],1,3000)
        else s1:=f[i-1];
        if length(f[i-2])>3000 then s2:=copy(f[i-2],length(f[i-2])-2999,3000)
        else s2:=f[i-2];
        f[i]:=s1+s2;
      end;
    for u:=1 to m do
      begin
        readln(s);
        l:=length(s);
        fillchar(c,sizeof(c),0);
        c[1]:=count(s,f[1]);
        c[2]:=count(s,f[2]);
       { c[3]:=count(s,f[3]);
        c[4]:=count(s,f[4]);}
        for i:=3 to n do
          begin
            ss:='';
            if length(f[i-1])>=l then
              s1:=copy(f[i-1],length(f[i-1])-(l-1)+1,l-1)
              else s1:=f[i-1];
            if length(f[i-2])>=l then
              s2:=copy(f[i-2],1,l-1)
              else s2:=f[i-2];
            ss:=s1+s2;
            c[i]:=(c[i-1]+c[i-2]+count(s,ss)) mod md;
          end;
        Writeln(c[n]);
      end;
  end.

const md = 1000000007;
var
ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
w,s:ansistring;
k:int64;
pp:array[0..200000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var cc,i,j,k:longint;
 begin
  if c<=0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then find:=x[1,2] else
  find:=x[1,1];
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  pp[1]:=0;
  for i:=2 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 30 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     p:=0;
     for j:=1 to 27 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=count;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

     j:=t+3;
     p4:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p4,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p4,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p4,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

const md = 1000000007;
var
io,p4,p3,p1,p2,t,p,j,i,m:longint;
w,s:ansistring;
k:int64;
ans,pp:array[0..200000]of longint;
f:array[0..10000]of ansistring;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  pp[1]:=0;
  for i:=2 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 30 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     p:=0;
     for j:=1 to 27 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=count;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

     j:=t+3;
     p4:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
 fillchar(ans,sizeof(ans),0);
     if (p1<>p2)and(s<>'a') then
       begin
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;

        // 1 1 1 1 1
       end else
     if (p1+p2=p3)or(s='a') then
       begin
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;

         // 0 0 0 0 0 0
       end else
       begin
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;

         // 1 0 1 0 1 0 1
       end;
     writeln(ans[k]);
    end;
 end.

{$R+}
const md = 1000000007;
var
uu,ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
anss,a1,a2,a3,a4,c1,t1,t2:array[0..100000]of int64;
w,s:ansistring;
k:int64;
pp:array[0..2000000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var ha,i,j,k:longint;
cc:int64;
 begin
  if c<=0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;
  for i:=1 to uu do
    if (p1=a1[i])and(p2=a2[i])and(p3=a3[i])and(p4=a4[i])
    and(c=c1[i])and(r1=t1[i])and(r2=t2[i]) then
      begin
       find:=anss[i];
                    exit;
      end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then
    begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,2];
     find:=x[1,2]
    end  else
       begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,1];
     find:=x[1,1];
    end;
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  for i:=length(s)+1 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
     if tt=4 then break;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 32 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     for i:=2 to length(s) do
       begin
     pp[i]:=pp[i-1];
     while (s[pp[i]+1]<>s[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if s[pp[i]+1]=s[i] then inc(pp[i]);

       end;
     p:=0;
     for j:=1 to 29 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=p;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p2+p3+1,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

const md = 1000000007;
var
ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
w,s:ansistring;
k:int64;
pp:array[0..1000000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var cc,i,j,k:longint;
 begin
  if c<=0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then find:=x[1,2] else
  find:=x[1,1];
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  pp[1]:=0;
  for i:=length(s)+1 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 30 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);

     for j:=2 to length(s) do
       begin
     pp[i]:=pp[i-1];
     while (s[pp[i]+1]<>s[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if s[pp[i]+1]=s[i] then inc(pp[i]);

       end;
     p:=0;
     for j:=1 to 27 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=p;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p2+p3+1,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

{$R+}
const md = 1000000007;
var
uu,ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
anss,a1,a2,a3,a4,c1,t1,t2:array[0..100000]of int64;
w,s:ansistring;
k:int64;
pp:array[0..2000000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var ha,i,j,k:longint;
cc:int64;
 begin
  if c<=0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;
  for i:=1 to uu do
    if (p1=a1[i])and(p2=a2[i])and(p3=a3[i])and(p4=a4[i])
    and(c=c1[i])and(r1=t1[i])and(r2=t2[i]) then
      begin
       find:=anss[i];
                    exit;
      end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then
    begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,2];
     find:=x[1,2]
    end  else
       begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,1];
     find:=x[1,1];
    end;
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  for i:=length(s)+1 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
     if tt=4 then break;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 27 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     for i:=2 to length(s) do
       begin
     pp[i]:=pp[i-1];
     while (s[pp[i]+1]<>s[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if s[pp[i]+1]=s[i] then inc(pp[i]);

       end;
     p:=0;
     for j:=1 to 27 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=p;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p2+p3+1,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

{$R+}
const md = 1000000007;
var
uu,ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
anss,a1,a2,a3,a4,c1,t1,t2:array[0..100000]of int64;
w,s:string;
k:int64;
pp:array[0..2000000]of longint;
f:array[0..10000]of string;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var ha,i,j,k:longint;
cc:int64;
 begin
  if c<=0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;
  for i:=1 to uu do
    if (p1=a1[i])and(p2=a2[i])and(p3=a3[i])and(p4=a4[i])
    and(c=c1[i])and(r1=t1[i])and(r2=t2[i]) then
      begin
       find:=anss[i];
                    exit;
      end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then
    begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,2];
     find:=x[1,2]
    end  else
       begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,1];
     find:=x[1,1];
    end;
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  for i:=length(s)+1 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
     if tt=4 then break;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 27 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     for i:=2 to length(s) do
       begin
     pp[i]:=pp[i-1];
     while (s[pp[i]+1]<>s[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if s[pp[i]+1]=s[i] then inc(pp[i]);

       end;
     p:=0;
     for j:=1 to 27 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=p;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p2+p3+1,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

{$R+}
const md = 1000000007;
var
uu,ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
anss,a1,a2,a3,a4,c1,t1,t2:array[0..100000]of int64;
w,s:ansistring;
k:int64;
pp:array[0..2000000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var ha,i,j,k:longint;
cc:int64;
 begin
  if c<=0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;
  for i:=1 to uu do
    if (p1=a1[i])and(p2=a2[i])and(p3=a3[i])and(p4=a4[i])
    and(c=c1[i])and(r1=t1[i])and(r2=t2[i]) then
      begin
       find:=anss[i];
                    exit;
      end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then
    begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,2];
     find:=x[1,2]
    end  else
       begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,1];
     find:=x[1,1];
    end;
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  for i:=length(s)+1 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
     if tt=4 then break;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 36 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     for i:=2 to length(s) do
       begin
     pp[i]:=pp[i-1];
     while (s[pp[i]+1]<>s[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if s[pp[i]+1]=s[i] then inc(pp[i]);

       end;
     p:=0;
     for j:=1 to 32 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=p;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p2+p3+1,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

{$R+}
const md = 1000000007;
var
uu,ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
anss,a1,a2,a3,a4,c1,t1,t2:array[0..100000]of int64;
w,s:ansistring;
k:int64;
pp:array[0..2000000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var ha,i,j,k:longint;
cc:int64;
 begin
  if c<=0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;
  for i:=1 to uu do
    if (p1=a1[i])and(p2=a2[i])and(p3=a3[i])and(p4=a4[i])
    and(c=c1[i])and(r1=t1[i])and(r2=t2[i]) then
      begin
       find:=anss[i];
                    exit;
      end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then
    begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,2];
     find:=x[1,2]
    end  else
       begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,1];
     find:=x[1,1];
    end;
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  for i:=length(s)+1 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
     if tt=4 then break;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 38 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     for i:=2 to length(s) do
       begin
     pp[i]:=pp[i-1];
     while (s[pp[i]+1]<>s[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if s[pp[i]+1]=s[i] then inc(pp[i]);

       end;
     p:=0;
     for j:=1 to 36 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=p;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p2+p3+1,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

{$R+}
const md = 1000000007;
var
uu,ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
anss,a1,a2,a3,a4,c1,t1,t2:array[0..100000]of int64;
w,s:ansistring;
k:int64;
pp:array[0..2000000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var ha,i,j,k:longint;
cc:int64;
 begin
  if c<=0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;
  for i:=1 to uu do
    if (p1=a1[i])and(p2=a2[i])and(p3=a3[i])and(p4=a4[i])
    and(c=c1[i])and(r1=t1[i])and(r2=t2[i]) then
      begin
       find:=anss[i];
                    exit;
      end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then
    begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,2];
     find:=x[1,2]
    end  else
       begin
     uu:=uu+1;
     a1[uu]:=p1;
     a2[uu]:=p2;
     a3[uu]:=p3;
     a4[uu]:=p4;
     c1[uu]:=cc;
     t1[uu]:=r1;
     t2[uu]:=r2;
     anss[uu]:=x[1,1];
     find:=x[1,1];
    end;
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  for i:=length(s)+1 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
     if tt=4 then break;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 30 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     for i:=2 to length(s) do
       begin
     pp[i]:=pp[i-1];
     while (s[pp[i]+1]<>s[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if s[pp[i]+1]=s[i] then inc(pp[i]);

       end;
     p:=0;
     for j:=1 to 27 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=p;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p2+p3+1,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p2+p3,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

const fin='';
    fout='';
var f:array [0..40] of ansistring;
    fs,fe:array [0..4000] of ansistring;
    fvh:array [0..4000] of  int64;
    ls,n,i,ii,k:longint; s:ansistring;
    fi,fo:text;
procedure wr (x:int64);
  begin
    writeln (fo,x);
  end;
function vh (s,ss:ansistring):int64;
var i,j,f:longint;ans:int64;
  begin
    ans:=0;
    for i:=1 to length(ss)-length(s)+1 do
      begin
        f:=0;
        for j:=1 to length(s) do
          if ss[i+j-1]<>s[j] then
            begin
                f:=1;break;
            end;
        if f=0 then
            ans:=(ans+1) ;
      end;
    vh:=ans ;
  end;
procedure xuli;
begin
    while not eof(fi) do
     begin
     f[1]:='a'; f[2]:='b';
     readln (fi,s);
     for i:=3 to 20 do f[i]:=f[i-1]+f[i-2];
     if n<=20 then
        begin
            wr(vh(s,f[n]));continue;
        end;
     ls:=length(s);
     fs[19]:=copy(f[19],1,ls-1);
     fs[20]:=copy(f[20],1,ls-1);
     fe[19]:=copy(f[19],length(f[19])-ls+2,ls-1);
     fe[20]:=copy(f[20],length(f[20])-ls+2,ls-1);
     fvh[19]:=vh(s,f[19]);
     fvh[20]:=vh(s,f[20]);
     for i:=21 to n do
       begin
          fs[i]:=fs[i-1];
          fe[i]:=fe[i-2];
          fvh[i]:=(fvh[i-1]+fvh[i-2]+vh(s,fe[i-1]+fs[i-2])) ;
       end;
     wr(fvh[n]);
     end;
end;
begin
     assign (fi,fin); reset(fi);
     assign (fo,fout); rewrite(fo);
     readln (fi,n,k);
     xuli;
     close(fi); close(fo);
end.

const fin='';
fout='';
var n:integer; s1,s2,s3,s:ansistring; ans2,ans1,ans3,k:longint;
f,g:text;
procedure tim(l,r:longint; st:ansistring; var ans:longint);
var i,j,lc:longint;
begin
     for i:=l to r do
       begin
        lc:=0;
        for j:=i to i+length(s)-1 do
            if st[j]<>s[j-i+1] then
              begin
                  lc:=1; break;
              end;
        if lc=0 then inc(ans);
       end;
end;
procedure xuli;
var i,j,l1,r1:longint;
begin
     ans1:=0; ans2:=0;
     tim(1,length(s1)-length(s)+1,s1,ans1);
     tim(1,length(s2)-length(s)+1,s2,ans2);
     ans3:=ans2+ans1;
     for i:=3 to n do
         begin
              s3:=s2+s1; ans3:=ans2+ans1;
              if length(s2)<length(s) then l1:=1
              else l1:=length(s2)-length(s)+2;
              if length(s1)<length(s) then r1:=length(s3)-length(s)+1
              else r1:=length(s2);
              if (l1>0) and (r1>0) and (length(s)>1) then tim(l1,r1,s3,ans3);
              s1:=s2; s2:=s3;
              ans1:=ans2; ans2:=ans3;
         end;
     writeln(g,ans3);
end;
begin
assign(f,fin);reset(f);
assign(G,fout);rewrite(g);
readln(f,n,k);
while not eof(f) do
begin
readln(f,s);
s1:='a'; s2:='b';
xuli;
end;
close(f); close(g);
end.

var f:array [0..4000] of ansistring;
    fs,fe:array [0..4000] of ansistring;
    fvh:array [0..4000] of int64;
    ls,n,i,ii,k:longint; s:string;
procedure wr (x:int64);
  begin
    writeln (x);
  end;
function vh (s,ss:ansistring):int64;
var i,j,f:longint;ans:int64;
  begin
    ans:=0;
    for i:=1 to length(ss)-length(s)+1 do
      begin
        f:=0;
        for j:=1 to length(s) do
          if ss[i+j-1]<>s[j] then begin f:=1;break;end;
        if f=0 then ans:=ans+1;
      end;
    vh:=ans mod 1000000007;
  end;

begin         {
assign (input,'input.txt');
assign (output,'output.txt');
reset (input);
rewrite (output);}
readln (n,k);
for ii:=1 to k do
begin
readln (s);
f[1]:='a';
f[2]:='b';
for i:=3 to 12 do f[i]:=f[i-1]+f[i-2];
if n<=12 then begin wr(vh(s,f[n]));continue;end;
ls:=length(s);
fs[11]:=copy(f[11],1,ls-1);
fs[12]:=copy(f[12],1,ls-1);
fe[11]:=copy(f[11],length(f[11])-ls+2,ls-1);
fe[12]:=copy(f[12],length(f[12])-ls+2,ls-1);
fvh[11]:=vh(s,f[11]);
fvh[12]:=vh(s,f[12]);
for i:=13 to n do
  begin
     fs[i]:=fs[i-1];
     fe[i]:=fe[i-2];
     fvh[i]:=(fvh[i-1]+fvh[i-2]+vh(s,fe[i-1]+fs[i-2])) mod 1000000007;
  end;
wr(fvh[n]);
end;
end.

var f:array [0..4000] of ansistring;
    fs,fe:array [0..4000] of ansistring;
    fvh:array [0..4000] of int64;
    ls,n,i,ii,k:longint; s:string;
procedure wr (x:int64);
  begin
    writeln (x);
  end;
function vh (s,ss:ansistring):int64;
var i,j,f:longint;ans:int64;
  begin
    ans:=0;
    for i:=1 to length(ss)-length(s)+1 do
      begin
        f:=0;
        for j:=1 to length(s) do
          if ss[i+j-1]<>s[j] then begin f:=1;break;end;
        if f=0 then ans:=ans+1;
      end;
    vh:=ans mod 1000000007;
  end;

begin          {
assign (input,'input.txt');
assign (output,'output.txt');
reset (input);
rewrite (output); }
readln (n,k);
for ii:=1 to k do
begin
readln (s);
f[1]:='A';
f[2]:='B';
for i:=3 to 12 do f[i]:=f[i-1]+f[i-2];
if n<=12 then begin wr(vh(s,f[n]));continue;end;
ls:=length(s);
fs[11]:=copy(f[11],1,ls-1);
fs[12]:=copy(f[12],1,ls-1);
fe[11]:=copy(f[11],length(f[11])-ls+2,ls-1);
fe[12]:=copy(f[12],length(f[12])-ls+2,ls-1);
fvh[11]:=vh(s,f[11]);
fvh[12]:=vh(s,f[12]);
for i:=13 to n do
  begin
     fs[i]:=fs[i-1];
     fe[i]:=fe[i-2];
     fvh[i]:=(fvh[i-1]+fvh[i-2]+vh(s,fe[i-1]+fs[i-2])) mod 1000000007;
  end;
wr(fvh[n]);
end;
end.

var f:array [0..4000] of ansistring;
    fs,fe:array [0..4000] of ansistring;
    fvh:array [0..4000] of int64;
    ls,n,i,ii,k:longint; s:ansistring;
procedure wr (x:int64);
  begin
    writeln (x);
  end;
function vh (s,ss:ansistring):int64;
var i,j,f:longint;ans:int64;
  begin
    ans:=0;
    for i:=1 to length(ss)-length(s)+1 do
      begin
        f:=0;
        for j:=1 to length(s) do
          if ss[i+j-1]<>s[j] then begin f:=1;break;end;
        if f=0 then ans:=ans+1;
      end;
    vh:=ans mod 1000000007;
  end;

begin         {
assign (input,'input.txt');
assign (output,'output.txt');
reset (input);
rewrite (output);}
readln (n,k);
for ii:=1 to k do
begin
readln (s);
f[1]:='a';
f[2]:='b';
for i:=3 to 12 do f[i]:=f[i-1]+f[i-2];
if n<=12 then begin wr(vh(s,f[n]));continue;end;
ls:=length(s);
fs[11]:=copy(f[11],1,ls-1);
fs[12]:=copy(f[12],1,ls-1);
fe[11]:=copy(f[11],length(f[11])-ls+2,ls-1);
fe[12]:=copy(f[12],length(f[12])-ls+2,ls-1);
fvh[11]:=vh(s,f[11]);
fvh[12]:=vh(s,f[12]);
for i:=13 to n do
  begin
     fs[i]:=fs[i-1];
     fe[i]:=fe[i-2];
     fvh[i]:=(fvh[i-1]+fvh[i-2]+vh(s,fe[i-1]+fs[i-2])) mod 1000000007;
  end;
wr(fvh[n]);
end;
end.

uses math;
const md=1000000009;
Var c:array[0..3003]of longint;
    f:array[0..3003]of ansistring;
    p:array[0..10333]of longint;
    s1,sss,s2,s,ss:ansistring;
    i,j,u,n,m:longint;
 function count(var s,ss:ansistring):longint;
 var i,j,res:longint;
   begin
     sss:=s+'$'+ss;
     p[1]:=0;
     res:=0;
     for i:=2 to length(sss) do
       begin
         j:=p[i-1];
         while (j>0)and(sss[j+1]<>sss[i]) do j:=p[j];
         if sss[j+1]=sss[i] then j:=j+1;
         p[i]:=j;
         if p[i]=length(s) then res:=res+1;
       end;
     count:=res;
   end;
  begin
    readln(n,m);
    f[1]:='a';
    f[2]:='b';
    for i:=3 to n do
      begin
        if length(f[i-1])>3000 then s1:=copy(f[i-1],1,3000)
        else s1:=f[i-1];
        if length(f[i-2])>3000 then s2:=copy(f[i-2],1,3000)
        else s2:=f[i-2];
        f[i]:=s1+s2;
      end;
    for u:=1 to m do
      begin
        readln(s);
        fillchar(c,sizeof(c),0);
        c[1]:=count(s,f[1]);
        c[2]:=count(s,f[2]);
        c[3]:=count(s,f[3]);
        c[4]:=count(s,f[4]);
        for i:=5 to n do
          begin
            ss:='';
            for j:=max(1,length(f[i-1])-length(s)+2) to length(f[i-1]) do
              ss:=ss+f[i-1][j];
            for j:=1 to min(length(f[i-2]),length(s)-1) do
              ss:=ss+f[i-2][j];
            c[i]:=(c[i-1]+c[i-2]+count(s,ss)) mod md;
          end;
        Writeln(c[n]);
      end;
  end.

uses math;
const md=1000000009;
Var c:array[0..3003]of longint;
    f:array[0..3003]of ansistring;
    p:array[0..10333]of longint;
    s1,sss,s2,s,ss:ansistring;
    i,j,u,n,m,l:longint;
 function count(var s,ss:ansistring):longint;
 var i,j,res:longint;
   begin
     sss:=s+'$'+ss;
     p[1]:=0;
     res:=0;
     for i:=2 to length(sss) do
       begin
         j:=p[i-1];
         while (j>0)and(sss[j+1]<>sss[i]) do j:=p[j];
         if sss[j+1]=sss[i] then j:=j+1;
         p[i]:=j;
         if p[i]=length(s) then res:=res+1;
       end;
     count:=res;
   end;
  begin
{    assign(input,'input.txt');
    reset(input); }
    readln(n,m);
    f[1]:='a';
    f[2]:='b';
    for i:=3 to n do
      begin
        if length(f[i-1])>3000 then s1:=copy(f[i-1],1,3000)
        else s1:=f[i-1];
        if length(f[i-2])>3000 then s2:=copy(f[i-2],1,3000)
        else s2:=f[i-2];
        f[i]:=s1+s2;
      end;
    for u:=1 to m do
      begin
        readln(s);
        l:=length(s);
        fillchar(c,sizeof(c),0);
        c[1]:=count(s,f[1]);
        c[2]:=count(s,f[2]);
       { c[3]:=count(s,f[3]);
        c[4]:=count(s,f[4]);}
        for i:=3 to n do
          begin
            ss:='';
            if length(f[i-1])>=l then
              s1:=copy(f[i-1],length(f[i-1])-(l-1)+1,l-1)
              else s1:=f[i-1];
            if length(f[i-2])>=l then
              s2:=copy(f[i-2],1,l-1)
              else s2:=f[i-2];
            ss:=s1+s2;
            c[i]:=(c[i-1]+c[i-2]+count(s,ss)) mod md;
          end;
        Writeln(c[n]);
      end;
  end.

uses math;
const md=1000000009;
Var c:array[0..3003]of longint;
    f:array[0..3003]of ansistring;
    p:array[0..10333]of longint;
    s1,sss,s2,s,ss:ansistring;
    i,j,u,n,m,l:longint;
 function count(var s,ss:ansistring):longint;
 var i,j,res:longint;
   begin
     sss:=s+'$'+ss;
     p[1]:=0;
     res:=0;
     for i:=2 to length(sss) do
       begin
         j:=p[i-1];
         while (j>0)and(sss[j+1]<>sss[i]) do j:=p[j];
         if sss[j+1]=sss[i] then j:=j+1;
         p[i]:=j;
         if p[i]=length(s) then res:=res+1;
       end;
     count:=res;
   end;
  begin
  {  assign(input,'input.txt');
    reset(input);}
    readln(n,m);
    f[1]:='a';
    f[2]:='b';
    for i:=3 to n do
      begin
        if length(f[i-1])>3000 then s1:=copy(f[i-1],1,3000)
        else s1:=f[i-1];
        if length(f[i-2])>3000 then s2:=copy(f[i-2],1,3000)
        else s2:=f[i-2];
        f[i]:=s1+s2;
      end;
    for u:=1 to m do
      begin
        readln(s);
        l:=length(s);
        fillchar(c,sizeof(c),0);
        c[1]:=count(s,f[1]);
        c[2]:=count(s,f[2]);
        c[3]:=count(s,f[3]);
        c[4]:=count(s,f[4]);
        for i:=5 to n do
          begin
            ss:='';
            if length(f[i-1])>=l then
              s1:=copy(f[i-1],length(f[i-1])-(l-1)+1,l-1)
              else s1:=f[i-1];
            if length(f[i-2])>=l then
              s2:=copy(f[i-2],1,l-1)
              else s2:=f[i-2];
            ss:=s1+s2;
            c[i]:=(c[i-1]+c[i-2]+count(s,ss)) mod md;
          end;
        Writeln(c[n]);
      end;
  end.

uses math;
const md=1000000007;
Var c:array[0..3003]of longint;
    f:array[0..3003]of ansistring;
    p:array[0..10333]of longint;
    s1,sss,s2,s,ss:ansistring;
    i,j,u,n,m,l:longint;
 function count(var s,ss:ansistring):longint;
 var i,j,res:longint;
   begin
     sss:=s+'$'+ss;
     p[1]:=0;
     res:=0;
     for i:=2 to length(sss) do
       begin
         j:=p[i-1];
         while (j>0)and(sss[j+1]<>sss[i]) do j:=p[j];
         if sss[j+1]=sss[i] then j:=j+1;
         p[i]:=j;
         if p[i]=length(s) then res:=res+1;
       end;
     count:=res;
   end;
  begin
{    assign(input,'input.txt');
    reset(input); }
    readln(n,m);
    f[1]:='a';
    f[2]:='b';
    for i:=3 to n do
      begin
        if length(f[i-1])>3000 then s1:=copy(f[i-1],1,3000)
        else s1:=f[i-1];
        if length(f[i-2])>3000 then s2:=copy(f[i-2],1,3000)
        else s2:=f[i-2];
        f[i]:=s1+s2;
      end;
    for u:=1 to m do
      begin
        readln(s);
        l:=length(s);
        fillchar(c,sizeof(c),0);
        c[1]:=count(s,f[1]);
        c[2]:=count(s,f[2]);
       { c[3]:=count(s,f[3]);
        c[4]:=count(s,f[4]);}
        for i:=3 to n do
          begin
            ss:='';
            if length(f[i-1])>=l then
              s1:=copy(f[i-1],length(f[i-1])-(l-1)+1,l-1)
              else s1:=f[i-1];
            if length(f[i-2])>=l then
              s2:=copy(f[i-2],1,l-1)
              else s2:=f[i-2];
            ss:=s1+s2;
            c[i]:=(c[i-1]+c[i-2]+count(s,ss)) mod md;
          end;
        Writeln(c[n]);
      end;
  end.

uses math;
const md=1000000009;
Var c:array[0..5003]of longint;
    f:array[0..5003]of ansistring;
    p:array[0..15333]of longint;
    s1,sss,s2,s,ss:ansistring;
    i,j,u,n,m:longint;
 function count(var s,ss:ansistring):longint;
 var i,j,res:longint;
   begin
     sss:=s+'$'+ss;
     p[1]:=0;
     res:=0;
     for i:=2 to length(sss) do
       begin
         j:=p[i-1];
         while (j>0)and(sss[j+1]<>sss[i]) do j:=p[j];
         if sss[j+1]=sss[i] then j:=j+1;
         p[i]:=j;
         if p[i]=length(s) then res:=res+1;
       end;
     count:=res;
   end;
  begin
    readln(n,m);
    f[1]:='a';
    f[2]:='b';
    for i:=3 to n do
      begin
        if length(f[i-1])>5000 then s1:=copy(f[i-1],1,5000)
        else s1:=f[i-1];
        if length(f[i-2])>5000 then s2:=copy(f[i-2],1,5000)
        else s2:=f[i-2];
        f[i]:=s1+s2;
      end;
    for u:=1 to m do
      begin
        readln(s);
        fillchar(c,sizeof(c),0);
        c[1]:=count(s,f[1]);
        c[2]:=count(s,f[2]);
        c[3]:=count(s,f[3]);
        c[4]:=count(s,f[4]);
        for i:=5 to n do
          begin
            ss:='';
            for j:=max(1,length(f[i-1])-length(s)+2) to length(f[i-1]) do
              ss:=ss+f[i-1][j];
            for j:=1 to min(length(f[i-2]),length(s)-1) do
              ss:=ss+f[i-2][j];
            c[i]:=(c[i-1]+c[i-2]+count(s,ss)) mod md;
          end;
        Writeln(c[n]);
      end;
  end.

const md = 1000000007;
var
ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
w,s:ansistring;
k:int64;
pp:array[0..200000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var cc,i,j,k:longint;
 begin
  if c<0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;

  cc:=c;

  c:=(c-4+1)div 2;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p4;
  e[1,2]:=p3;
  e[1,3]:=p2;
  e[1,4]:=p1;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

 { for i:=1 to 6 do write(x[1,i],' ');writeln;

  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;       }

  if odd(cc) then find:=x[1,2] else
  find:=x[1,1];
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  pp[1]:=0;
  for i:=2 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 30 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,0,1));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     p:=0;
     for j:=1 to 27 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=count;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

     j:=t+3;
     p4:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p4,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p4,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p4,k-t+1,0,1);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.

const md = 1000000007;
var
io,p4,p3,p1,p2,t,p,j,i,m:longint;
w,s:ansistring;
k:int64;
ans,pp:array[0..200000]of longint;
f:array[0..10000]of ansistring;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  pp[1]:=0;
  for i:=2 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 30 do
    f[i]:=f[i-1]+f[i-2];

{  s:='a';
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 27 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
           }
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     p:=0;
     for j:=1 to 27 do
       begin
        if length(s[j])>length(s)*4 then break;

        p:=count;
        t:=j;
        if p>0 then break;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=count;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

     j:=t+3;
     p4:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);

     if (p1<>p2)and(s<>'a') then
       begin
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;

        // 1 1 1 1 1
       end else
     if (p1+p2=p3)or(s='a') then
       begin
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;

         // 0 0 0 0 0 0
       end else
       begin
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;

         // 1 0 1 0 1 0 1
       end;
     writeln(ans[k]);
    end;
 end.

const md = 1000000007;
var
ans,io,p4,p3,p1,p2,t,p,j,i,m:longint;
w,s:ansistring;
k:int64;
pp:array[0..200000]of longint;
f:array[0..10000]of ansistring;
x,e,ed:array[0..6,0..6]of int64;

function find(p1,p2,p3,p4:longint;c:int64;r1,r2:longint):longint;
var cc,i,j,k:longint;
 begin
  if c<0 then
    begin
     find:=0;
     exit;
    end;
  if c=1 then
    begin
     find:=p1;
     exit;
    end;
  if c=2 then
    begin
     find:=p2;
     exit;
    end;
  if c=3 then
    begin
     find:=p3;
     exit;
    end;
  if c=4 then
    begin
     find:=p4;
     exit;
    end;

  cc:=c;

  c:=(c-4+1)div 2+1;

  fillchar(x,sizeof(x),0);
  x[1,1]:=2;x[1,2]:=1;x[1,3]:=1;
  x[2,1]:=1;x[2,2]:=1;x[2,4]:=1;
  x[5,1]:=1;x[5,5]:=1;
  x[6,1]:=1;x[6,2]:=1;x[6,6]:=1;
  fillchar(ed,sizeof(ed),0);
  for i:=1 to 6 do
    ed[i,i]:=1;

  while c>0 do
    begin
     if c mod 2=1 then
       begin
        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+ed[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            ed[i,j]:=e[i,j];
       end;

        for i:=1 to 6 do
          for j:=1 to 6 do
            begin
             e[i,j]:=0;
             for k:=1 to 6 do
               e[i,j]:=(e[i,j]+x[i,k]*x[k,j])mod md;
            end;
        for i:=1 to 6 do
          for j:=1 to 6 do
            x[i,j]:=e[i,j];

     c:=c div 2;
    end;

  fillchar(e,sizeof(e),0);
  e[1,1]:=p1;
  e[1,2]:=p2;
  e[1,3]:=p3;
  e[1,4]:=p4;
  e[1,5]:=r1;
  e[1,6]:=r2;

  fillchar(x,sizeof(x),0);

  for i:=1 to 1 do
    for j:=1 to 6 do
      for k:=1 to 6 do
        x[i,j]:=(x[i,j]+e[i,k]*ed[k,j])mod md;

//  for i:=1 to 6 do write(x[1,i],' ');writeln;

{  for i:=1 to 6 do
   begin
    for j:=1 to 6 do
      write(ed[i,j],' ');
    writeln;
   end;}

  if odd(cc) then find:=x[1,2] else
  find:=x[1,1];
 end;

function count:longint;
var tt,i:longint;
 begin
  w:=s+'#'+f[j];
  tt:=0;
  pp[1]:=0;
  for i:=2 to length(w) do
    begin
     pp[i]:=pp[i-1];
     while (w[pp[i]+1]<>w[i])and(pp[i]>0) do pp[i]:=pp[pp[i]];

     if w[pp[i]+1]=w[i] then inc(pp[i]);

     if pp[i]=length(s) then tt:=tt+1;
    end;
  count:=tt;
 end;

 begin
  f[1]:='a';
  f[2]:='b';
  for i:=3 to 30 do
    f[i]:=f[i-1]+f[i-2];

{  readln(s);
  p1:=0;
  p2:=0;
  p3:=0;
  for j:=1 to 22 do
    begin
     p3:=p2;
     p2:=p1;
     p1:=count;
     writeln(j,' ',p1,' ',p1-p2-p3);
    end;
       halt(0);
              }
  //            readln(k);
//       writeln(find(1,1,3,4,k,1,0));halt(0);
  readln(k,m);
  for io:=1 to m do
    begin
     readln(s);
     p:=0;
     for j:=1 to 27 do
       begin
        if length(f[j])>length(s)*4 then break;

        if length(s)<=length(f[j]) then
         begin
          p:=count;
          t:=j;
          if p>0 then break;
         end;
       end;

     if p = 0 then
       begin
        writeln(0);
        continue;
       end;

     j:=t;
     p1:=count;

     j:=t+1;
     p2:=count;

     j:=t+2;
     p3:=count;

     j:=t+3;
     p4:=count;

    // writeln(t,' ',p1,' ',p2,' ',p3,' ',p4);
    if s='a' then
      begin
       if k=1 then ans:=1 else
       if k=2 then ans:=0 else
       ans:=find(1,1,2,3,k-2,0,0);
      end else
     if (p1<>p2)and(s<>'a') then
       begin
        {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md;
          end;
         }

        ans:=find(p1,p2,p3,p4,k-t+1,1,1);
        // 1 1 1 1 1
       end else
     if (p1+p2=p3) then
       begin       {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                    }

        ans:=find(p1,p2,p3,p4,k-t+1,0,0);
         // 0 0 0 0 0 0
       end else
       begin         {
        ans[t]:=p1;
        ans[t+1]:=p2;
        ans[t+2]:=p3;

        for i:=t+3 to k do
          begin
           if odd(i) = odd(t) then
           ans[i]:=(ans[i-1]+ans[i-2]+1)mod md else
           ans[i]:=(ans[i-1]+ans[i-2])mod md;
          end;
                      }

        ans:=find(p1,p2,p3,p4,k-t+1,1,0);
         // 1 0 1 0 1 0 1
       end;
     writeln(ans);
    end;
 end.