Let's consider one interesting word game. In this game you should transform one word into another through special operations. Let's say we have word w, let's split this word into two non-empty parts x and y so, that w = xy. A split operation is transforming word w = xy into word u = yx. For example, a split operation can transform word "wordcut" into word "cutword".You are given two words start and end. Count in how many ways we can transform word start into word end, if we apply exactly k split operations consecutively to word start. Two ways are considered different if the sequences of applied operations differ. Two operation sequences are different if exists such number i (1 ≤ i ≤ k), that in the i-th operation of the first sequence the word splits into parts x and y, in the i-th operation of the second sequence the word splits into parts a and b, and additionally x ≠ a holds.

The first line contains a non-empty word start, the second line contains a non-empty word end. The words consist of lowercase Latin letters. The number of letters in word start equals the number of letters in word end and is at least 2 and doesn't exceed 1000 letters. The third line contains integer k (0 ≤ k ≤ 105) — the required number of operations.

Print a single number — the answer to the problem. As this number can be rather large, print it modulo 1000000007 (109 + 7).

NoteThe sought way in the first sample is:ab  →  a|b  →  ba  →  b|a  →  abIn the second sample the two sought ways are: ababab  →  abab|ab  →  ababab  ababab  →  ab|abab  →  ababab

const md = 1000000007;
var
ans,p1,p2,np1,np2:int64;
s,w:ansistring;
n,i,k:longint;
 begin
  readln(s);
  readln(w);
  n:=length(s);
  readln(k);

  p1:=1;
  p2:=0;

  for i:=1 to k do
    begin
     np1:=p2*(n-1);
     np2:=p1+p2*(n-2);

     p1:=np1 mod md;
     p2:=np2 mod md;
    end;

  ans:=0;

  if s=w then ans:=(ans+p1)mod md;

  for i:=1 to n-1 do
    begin
     s:=s+s[1];
     delete(s,1,1);
     if s=w then ans:=(ans+p2)mod md;
    end;
  writeln(ans);
 end.

uses math;
const debug=0;
      oo=high(longint);
      eps=1e-5;
      dx:array[0..7] of longint=(0,0,1,-1,-1,-1,1,1);
      dy:array[0..7] of longint=(-1,1,0,0,-1,1,-1,1);
      modd=round(1e9)+7;
var fi,fo:string;
    f,g:text;
    a,b:ansistring;
    ans,gt,k,n:int64;
    i:longint;
    s:array[1..2,0..110000] of int64;
    //-----------
begin
    randomize;
    fi:=''; fo:='';
    if debug=1 then begin fi:='i.inp'; fo:='o.out'; end;
    assign(f,fi); reset(F);
    assign(g,fo); rewrite(G);
    //
        readln(f,a); n:=length(a);
        readln(f,b);
        readln(f,k);
        if length(a)<>length(b) then begin writeln(g,0); close(f); close(g); halt; end;

        s[1,0]:=1;
        for i:=1 to k do begin
            s[1,i]:=((n-1)*s[2,i-1]+(n-2)*s[1,i-1]) mod modd;
            s[2,i]:=s[1,i-1];
        end;
        for i:=1 to n-1 do
         if copy(a,i+1,n-i)+copy(a,1,i)=b then ans:=(ans+s[2,k]) mod modd;

        s[1,0]:=0; s[2,0]:=1;
        for i:=1 to k do begin
            s[1,i]:=((n-1)*s[2,i-1]+(n-2)*s[1,i-1]) mod modd;
            s[2,i]:=s[1,i-1];
        end;
        if a=b then ans:=(ans+s[2,k]) mod modd;
        writeln(g,ans);
    close(g);
    close(f);
end.

const
  base=1000000007;

var
  f:array[0..500500,1..2]of longint;
  s,p,q:ansistring;
  n,i,k,ans:longint;

begin
  readln(s);
  readln(p);
  readln(k);
  n:=length(s);
  f[0][1]:=1;
  f[0][2]:=0;
  for i:=1 to k do begin
    f[i][1]:=int64(f[i-1][2])*(n-1)mod base;
    f[i][2]:=int64(f[i-1][2])*(n-2)mod base;
    f[i][2]:=(f[i][2]+f[i-1][1])mod base;
  end;
  if (s=p)then ans:=f[k][1] else ans:=0;
  q:=s;
  for i:=2 to length(s)do begin
    delete(q,1,1);
    q:=q+s[i-1];
    if (q=p)then ans:=(ans+f[k][2])mod base;
  end;
  writeln(ans);
end.

const modulo = 1000000007;

var a, b ,  aa ,bb,ans :int64;
    st , fin : array [1..1009] of char;
    n ,k :longint;

function equal( s : longint ): boolean;
var i,j:longint;
begin
  i:=s+1;
  if i > n then i:=1;
  j:=2;

  while (i<>s) and (st[j] = fin[i] ) do begin
    inc(j);
    inc(i);
    if i > n then i:=1;
  end;

  if (st[1] <> fin[i]) or (j<>n+1) then exit(false) else exit(true);
end;

var i :longint;
    c:char;
begin
  a:=1;
  b:=0;
  i:=0;

  while not EOLN do begin inc(i); read(st[i]); end;
  n:=i;
  i:=0;
  readln(c);
  for i:=1 to n do read(fin[i]);

  readln( k );

  ans:=0;
  for i:=1 to k do begin
    aa:=a; bb:=b;
    a:=bb*(n-1);
    b:=aa + bb*(n-2);
    a:=a mod  modulo;
    b:=b mod  modulo;
  end;

  //writeln(a,b);
  for i:=1 to n do begin
    if equal(i) then begin
      if i = 1 then inc(ans , a) else inc(ans , b);
    end;
    ans:= ans mod modulo;
  end;
  //writeln(equal(1));
  writeln( ans );

end.

program qwe;
{$APPTYPE CONSOLE}
{$MAXSTACKSIZE $00200000}

uses
  SysUtils,
  Math;

//it is a big shablon. if you see any strange function, never mind

const base = 1000000007;
 eps = 1e-8;
 inf = 1 shl 30;

type
 TArray = array[0..10000010] of integer;
 int53 = int64;
 itneger = integer;
 point = record
  x, y: integer;
 end;

function sqrr(a, b: integer): int64;
var
 dega, res: int64;
begin
 res := 1;
 dega := a;
 while b > 0 do
 begin
  if odd(b) then res := res * dega;
  dega := dega * dega;
  b := b div 2;
  res := res mod base;
  dega := dega mod base;
 end;
 result := res mod base;
end;

function less(x, y: point): boolean;
begin
 if (x.x < y.x) or ((x.x = y.x) and (x.y < y.y)) then result := true else result := false;
end;

procedure swap(var x, y: integer);
var
 z: integer;
begin
 z := x;
 x := y;
 y := z;
end;

procedure swap64(var x, y: int64);
var
 z: int64;
begin
 z := x;
 x := y;
 y := z;
end;

procedure swapex(var x, y: extended);
var
 z: extended;
begin
 z := x;
 x := y;
 y := z;
end;

procedure swapp(var x, y: point);
var
 z: point;
begin
 z := x;
 x := y;
 y := z;
end;

var
 s, t: string;
 b: array[0..1010] of boolean;
 i, j, n, k: integer;
 x, y, xx, yy, ans: int64;

begin
 {$IFNDEF ONLINE_JUDGE}
 reset(input,'input.txt');
 rewrite(output, 'output.txt');
 {$ENDIF}
 readln(s);
 readln(t);
 n := length(s);
 for i := 1 to n do
 begin
  b[i - 1] := true;
  for j := 0 to n - 1 do
  b[i - 1] := b[i - 1] and (s[j + 1] = t[(i + j - 1) mod n + 1]);
 end;
 readln(k);
 ans := 0;
 if k = 0 then
 begin
  if s = t then
  writeln(1) else
  writeln(0);
  halt(0);
 end else
 if k = 1 then
 begin
  for i := 2 to n do
  if b[i - 1] then inc(ans);
  writeln(ans);
  halt(0);
 end;
 if n = 2 then
 begin
  if (s = t) and (s[1] = s[2]) then writeln(1) else
  if s = t then writeln(1 - k mod 2) else
  if (s[1] = t[2]) and (s[2] = t[1]) then writeln(k mod 2) else
  writeln(0);
  halt(0);
 end;
 dec(k, 2);
 x := n - 1;
 y := n - 2;
 for i := 1 to k do
 begin
  xx := y * (n - 1) mod base;
  yy := y * (n - 2) mod base;
  yy := (yy + x) mod base;
  x := xx;
  y := yy;
 end;
 for i := 1 to n - 1 do
 if b[i] then ans := (ans + y) mod base;
 if b[0] then ans := (ans + x) mod base;
 writeln(ans);
end.

program pr1;

{$APPTYPE CONSOLE}

     type double=int64;
const nn=1000001; modul=1000000007;
 eps=0.0000000001;  nnn=2000000;
 label 1,2,3,4;
var kol,j1,i,kk,pp,t,tt,k,i3,i0,i1,i2,p3,pgor,pver,step,j:longint;
p1,p,kolraz,rr,s1,mmin,imin,x0,y0,w,vp,vd,f,ii,n,ot,p2,besk,l,m,k1,k2,a2,jj:int64;
koef:double;  ch,ch1,ch2:char;
sdvig,vrag:array [-1..100000 ] of boolean;
x,y,gr:array[0..nn] of double;
s:double;
kol0,kol_0,dd:array[0..100000] of int64;
 oldmingor,oldminver,mingor,minver:array[0..1000] of longint;
sss,sss1,sss2,sss3: string;

a1,b1:array[0..200,0..200] of char;
aa:array[0..200,0..200] of boolean;
ss:array[0..30000] of string;
flag2,flag1,flag:boolean;
verot:double;
name:array[0..200] of string;
function min (a,b:longint):longint;begin min:=b;if a<b then min:=a;  end;
function max (a,b:longint):longint;begin max:=b;if a>b then max:=a;  end;

function nod(a,b:longint):longint; var p,t:longint;
begin if (a>0)and(b>0) then
   begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
    p:=a;
   end else if a=0 then p:=b else p:=a;
nod:=p;
end;

 function luck(a:longint):boolean;
 var i,j,k:longint;flag:boolean;
 c: array[0..10] of longint;
begin
  flag:=true;  j:=0;
  k:=a;
  while k>0 do begin inc(j); c[j]:=k mod 10; k:= k div 10; {writeln(j,' uu ',k); }  end;
 for i:=1 to j do if (c[i]<>4)and(c[i]<>7) then flag:=false;
 luck:=flag;
end;

  { function dist(i0,i1:longint):longint;
   var i,j,k,mmin,imin:longint; pr:array[0..3000] of boolean;
      dd:array[0..10000] of longint;
 begin  for i:=1 to n do pr[i]:=false;
   pr[i0]:=true;  dd[i0]:=0;
   for i:=1 to n do if not pr[i] then dd[i]:=gr[i0,i];
     k:=1;
       while k<n do
    begin
       inc(k);
        imin:=1; mmin:=1000000;
        for i:=1 to n do if (dd[i]<mmin) and (not pr[i]) then
          begin  imin:=i;mmin:=dd[i];   end;
       pr[imin]:=true;
       for i:=1 to n do if not pr[i] then dd[i]:=min(dd[i],dd[imin]+gr[imin,i]);

    end;

 dist:=dd[i1];

 end;  }

   function ttt(ss:string):longint; var i,j,k,s:longint; fl:boolean;
 begin k:=length(ss);
   s:=0; for i:=1 to k do s:=10*s+ord(ss[i])-48;
      ttt:=s;
 end;

         begin
{assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
}  readln(sss); readln(sss1); readln(kol);
    n:=length(sss);
    sss:=sss+sss; sss1:=sss1+sss1;
     { writeln(sss);  writeln(sss1);  writeln(n);
   } for i:=0 to n-1 do
      begin
         flag:=true;
           for j:=1 to n do if sss[i+j]<>sss1[j] then
           begin flag:=false; {writeln('j= ',j);} end;
         sdvig[i]:=flag;
      end;
      if (kol=0)and sdvig[0] then begin writeln(1);goto 1;      end;
     { for i:=0 to n-1 do write(sdvig[i],' '); writeln;
     }  kol0[1]:=0;kol_0[1]:=1;
     for i:=2 to kol do
       begin
          p:=n;
          kol0[i]:=((p-1)*kol_0[i-1]) mod modul;
          kol_0[i]:=((p-2)*kol_0[i-1]+kol0[i-1]) mod modul;
       end;
     { for i:=1 to kol do writeln(i,' ',kol0[i]:7,' ',kol_0[i]);
      writeln(kol0[kol],'   ==  ',kol_0[kol]);
     } p:=0;
       for i:=1 to n-1 do if sdvig[i] then inc(p);
        if sdvig[0] then p1:=1 else p1:=0;
      ot:=(p*kol_0[kol]+p1*kol0[kol]) mod modul;
                 writeln(ot);

   1:
            close(output);
end.

{$APPTYPE CONSOLE}

uses
  Math, SysUtils;

var
  f1, f2 : array [0.. 200000] of int64;
  a, b, s : string;
  i, k : Integer;
  ans : int64;

begin
  //reset(input, 'input.txt');
  //rewrite(output, 'output.txt');
  readln(a);
  readln(b);
  read(k);
  for i := 0 to k do begin
    f1[i] := 0;
    f2[i] := 0;
  end;

  f1[0] := 0;
  f1[1] := 1;
  f2[0] := 1;
  f2[1] := 0;
  for i := 2 to k do begin
    f1[i] := (f1[i - 1] * (length(a) - 2) + f2[i - 1]) mod 1000000007;
    f2[i] := f1[i - 1] * (length(a) - 1) mod 1000000007;
  end;

  ans := 0;
  for i := 1 to length(a) - 1 do begin
    s := copy(a, i + 1, length(a) - i) + copy(a, 1, i);
    if s = b then
      ans := (ans + f1[k]) mod 1000000007;
  end;

  if a = b then
    ans := (ans + f2[k]) mod 1000000007;

  write(ans);
end.

program Task1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
const md = round(1e9)+7;
var t,s1,s2: string;
    n,i,col,k,j,l: integer;
    cnt0,cnt1,buf0,buf1: integer;
    r: array[0..1000]of integer;
begin
  readln(s1);
  readln(s2);
  readln(k);
  col:=0;
  if (s1=s2)then begin
    inc(col);
    r[col]:=0;
  end;
  n:=length(s1)-1;
  for i:=1 to n do begin
    t:=copy(s1,i+1,n+1-i)+copy(s1,1,i);
    if (t=s2)Then begin
      inc(col);
      r[col]:=i;
    end;
  end;
  if (col=0)then begin
    write(0);
    halt;
  end;

  cnt0:=1;
  cnt1:=0;

  for i:=1 to k do begin
    buf0:=cnt0;
    buf1:=cnt1;
    cnt0:=0;
    for j:=1 to n do
      cnt0:=(cnt0+buf1)mod md;
    cnt1:=buf0;
    for j:=1 to n-1 do
      cnt1:=(cnt1+buf1)mod md;
  end;

  j:=0;
  for i:=1 to col do
    if (r[i]=0)then
      j:=(j+cnt0)mod md
    else
      j:=(j+cnt1)mod md;
  write(j);
end.

program Project2;

{$APPTYPE CONSOLE}
uses
  SysUtils;

const
  base = 1000000007;

var
  k, i, j :integer;
  a, b:string;
  d:array[0..100001, 0..1] of int64;
  n:int64;
  ans:int64;
  flag:boolean;

begin
  //set(input, 'input.txt');
  //write(output, 'output.txt');
  readln(a);
  readln(b);
  readln(k);
  ans := 0;
  n := length(a);
  d[0][0] := 1;
  for i := 1 to k do begin
    d[i][0] := (d[i - 1][1] * (n - 1)) mod base;
    d[i][1] := (d[i - 1][0] + d[i - 1][1] * (n - 2)) mod base;
  end;
  if a = b then
    ans := d[k][0];
  a := a + a;
  for i := 2 to n do begin
    flag := true;
    for j := 1 to n do
      if b[j] <> a[i + j - 1] then begin
        flag := false;
        break;
      end;
    if flag then
      ans := (ans + d[k][1]) mod base;
  end;
  write(ans);

end.

{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O-,P+,Q+,R+,S+,T-,U-,V+,W-,X+,Y+,Z1}
{$MINSTACKSIZE $00004000}
{$MAXSTACKSIZE $00100000}
{$IMAGEBASE $00400000}
{$APPTYPE CONSOLE}

const file1='b.in';
      file2='b.out';
      oo=maxlongint div 2;
      empty='';
      eps=1e-6;
      dohrena=1e21;
      modulo=1000000007;

var qc:array[0..200000,0..1]of int64;
    ss:array[0..2009]of string;
    qq,cc:array[0..500000]of longint;
    bb:array[0..500000]of boolean;
    xx,yy,zz:array[0..500000]of extended;
    k,ii,jj,kk,ll,t,test,u,v,w,n,m,r,p,q,c,h,e,r0,r1,r2,res,num:longint;
    a:int64;
    s,s0,s1,s2:ansistring;
    sss:ansistring;
    x,y,z,x0,x1,x2,y0,y1,y2,z0,z1,z2:extended;
    ch:char;
    b,b0,b1,b2:boolean;

procedure clear;
var i,j:longint;
    begin
    fillchar(qc,sizeof(qc),0);
    end;

procedure load;
var i,j:longint;
    begin
    readln(s1);
    readln(s2);
    readln(n);
    end;

procedure save;
var i,j:longint;
    begin
    writeln(qc[n,1]);
    end;

procedure run;
var i,j:longint;
    begin
    num:=0;
    ll:=length(s1);
    for i:=1 to ll do
      begin
      s0:='';
      for j:=i to ll do s0:=s0+s1[j];
      for j:=1 to i-1 do s0:=s0+s1[j];
      if s0=s2 then inc(num);
      end;
    if s1=s2 then qc[0,1]:=1 else qc[0,0]:=1;
    for i:=1 to n do
      begin
      qc[i,0]:=(qc[i-1,0]*(ll-num-1)+qc[i-1,1]*(ll-num))mod modulo;
      qc[i,1]:=(qc[i-1,0]*num+qc[i-1,1]*(num-1))mod modulo;
      end;
    end;

begin
{assign(input,file1);
assign(output,file2);
reset(input);
rewrite(output);}
for t:=1 to 1 do
  begin
  clear;
  load;
  run;
  save;
  end;
{close(input);
close(output);}
end.

const modulo = 1000000007;
var f:array[0..111111] of int64;
    n,i,j:longint;
    s1,s2:ansistring;
    sum,answer:int64;

function solve(d:longint; var s1,s2:ansistring):boolean;
var i1,i2:longint;
    i:longint;
begin
 for i:= 1 to length(s1) do
  begin
   j:=i+d;
   j:=j mod length(s1); if j = 0 then j:=length(s1);
   if s1[j]<>s2[i] then
    begin
     solve:=false;
     exit;
    end;
  end;
 solve:=true;
end;

begin
 {assign(input,'input.txt');
 reset(input); }
 readln(s1);
 readln(s2);
 readln(n);
 fillchar(f,sizeof(f),0);
 f[0]:=1;
 sum:=1;
 for i:= 1 to n do
  begin
   for j:= 0 to length(s1)-1 do
   begin
    f[j]:=sum-f[j];
    if f[j]<0 then f[j]:=f[j]+modulo;
   end;
   sum:=0;
   for j:= 0 to length(s1)-1 do
    sum:=(Sum+f[j]) mod modulo;
  end;
 answer:=0;
 for i:= 0 to length(s1)-1 do
  begin
   if solve(i,s1,s2)=false then continue;
   answer:=(answer+f[i]) mod modulo;
  end;
 writelN(answer);
end.

{$R+,Q+,I+}
{$apptype console}
Uses SysUtils;
Var
   s1,s2:String;
   pref,suf:Array[0..3000] of String;
   i,md:Integer;
   k,t,r,r1,r2,ans,n:Int64;
Begin
   md:=1000000007;
   ReadLn(s1);
   ReadLn(s2);
   Read(k);
   If k=0 Then Begin
      If s1=s2 Then Write(1)
               Else Write(0);
      Halt;
   End;
   r1:=0;
   r2:=0;
   n:=length(s1);
   pref[1]:=s1[1];
   For i:=2 To n Do pref[i]:=pref[i-1]+s1[i];
   suf[n]:=s1[n];
   For i:=n-1 DownTo 1 Do suf[i]:=s1[i]+suf[i+1];
   If s1=s2 Then r1:=1;
   For i:=2 To n Do
      If suf[i]+pref[i-1]=s2 Then r2:=r2+1;
   If k=1 Then Begin
      Write(r2);
      Halt;
   End;
   t:=1;
   r:=1;
   For i:=1 To k Do Begin
      t:=(md+r-t) mod md;
      r:=(r*(n-1)) mod md;
   End;
   ans:=t*r1 mod md;
   t:=0;
   r:=1;
   For i:=1 To k Do Begin
      t:=(md+r-t) mod md;
      r:=(r*(n-1)) mod md;
   End;
   ans:=(ans+t*r2) mod md;
   Write(ans);
End.

{$APPTYPE CONSOLE}
{$Q+,R+,S+}

const   maxl = 1000;
        MD = 1000000007;

var l: integer;
var k: integer;
var start, finish: string;
var x: array[1..maxl] of int64;

procedure readdata;
begin
    readln(start);
    readln(finish);
    read(k);
end;

procedure init;
begin
    l := length(start);
end;

var a, b: int64;

procedure solve;
var i: integer;
    ta, tb: int64;
begin
    a := 1; b := 0;
    for i := 1 to k do begin
        ta := (b * (l - 1)) mod MD;
        tb := (b * (l - 2) + a) mod MD;
        a := ta;
        b := tb;
    end;
end;

procedure save;
var
    ans: int64;
    i: integer;
begin
    ans := 0;
    for i := 1 to l do begin
        if copy(start, i, l-i+1) + copy(start, 1, i-1) = finish then
            if i = 1 then
                ans := ans + a
            else
                ans := ans + b
    end;
    write(ans mod MD)
end;

begin
    {$IFNDEF ONLINE_JUDGE}
    assign(input, 'b.in'); reset(input);
    assign(output, 'b.out'); rewrite(output);
    {$ENDIF}
    readdata;
    init;
    solve;
    save;
    {$IFNDEF ONLINE_JUDGE}
    close(output);
    close(input);
    {$ENDIF}
end.

program cf176b;
const mo=1000000007;
var
  n,m,i,j,kk:longint;
  jz:array[0..3000]of int64;
  s1,s2:array[0..3000]of char;
  f0,h1,h2,f1:array[0..200000]of int64;
  ct0,ct1:int64;
  ans:int64;
begin
  jz[0]:=1;
  for i:=1 to 3000 do jz[i]:=jz[i-1]*29;

  n:=0;
  while not(eoln) do begin
    inc(n);
    read(s1[n]);
  end;
  readln;
  m:=0;
  while not(eoln) do begin
    inc(m);
    read(s2[m]);
  end;     readln;
  if n<>m then begin
    writeln(0);
    halt;
  end;

  for i:=m+1 to 2*m do s2[i]:=s2[i-m];

  h2[0]:=0;
  for i:=1 to 2*m do h2[i]:=(h2[i-1]*29+(ord(s2[i])-ord('a')+1) );
  h1[0]:=0;
  for i:=1 to n do h1[i]:=(h1[i-1]*29+(ord(s1[i])-ord('a')+1) );
  if h2[m]=h1[n] then ct0:=1 else ct0:=0;

  ct1:=0;
  for i:=m+1 to 2*m-1 do
    if h2[i]-(h2[i-m]*jz[m])=h1[m] then inc(ct1);

  f0[0]:=1;
  f1[0]:=0;
  readln(kk);
  for i:=1 to kk do begin
    f0[i]:=(f1[i-1]*(n-1)) mod mo;
    f1[i]:=(f0[i-1]+f1[i-1]*(n-2))mod mo;
  end;

  if ct0=1 then ans:=f0[kk] else ans:=0;
  ans:=((ans+f1[kk]*ct1)mod mo);
  writeln(ans);
end.

{$Mode objfpc}
{$M 20000000}
{$H+}

const
    maxN= 100000;
    base= round(1e9)+ 7;

var
    f, g: array[0.. maxn] of int64;
    i, j, k, n, d: integer;
    res: int64;
    s, t, x: string;

begin
  //  assign(input, 'test.inp'); reset(input);

    readln(s);
    readln(t);
    n:= length(t);
    readln(k);
    x:= s;
    for i:= 2 to n do
        begin
            x:= x+ x[1];
            delete(x, 1, 1);
            if x= t then inc(d);
        end;
    g[0]:= 1;
    f[1]:= 1;
    g[1]:= 0;
    for i:= 2 to k do
        begin
            f[i]:= ( (n- 2)* f[i- 1]+ g[i- 1]) mod base;
            g[i]:= f[i- 1]* (n- 1) mod base;
        end;
    res:= f[k]* d mod base;
    if s= t then res:= (res+ g[k]) mod base;
    writeln(res);
end.

var
        st1,st2:ansistring;
        a,b,a1,b1,n,ans:int64;
        i,k,j:longint;
begin
        readln(st1);
        readln(st2);
        readln(k);
        a:=1;
        b:=0;
        n:=length(st1);
        for i:=1 to k do begin
        a1:=b*(n-1) mod 1000000007;
        b1:=(a+b*(n-2)) mod 1000000007;
        a:=a1;
        b:=b1;
        end;
        ans:=0;
        if st1=st2 then ans:=ans+a;
        for i:=1 to n-1 do
        begin
        ans:=(ans+b)mod 1000000007;

        for j:=0 to n-1 do
        if st1[(i+j) mod n +1]<>st2[(j) mod n +1] then
        begin
        ans:=(ans-b+1000000007)mod 1000000007;
        break;
        end;
        end;
        writeln(ans);

end.

const mo=1000000007;
var i,j,q:longint;
    n,t,t2,a,a2,ans:int64;
    v:array[0..1000]of boolean;
    s,w:array[1..2000]of char;
//======================================================
begin
  repeat
    inc(n);read(s[n]);
  until eoln;readln;
  for i:=1 to n do read(w[i]);
  readln(q);
  for i:=1 to n do s[i+n]:=s[i];
  for i:=0 to n-1 do
    begin
      v[i]:=true;
      for j:=1 to n do
        if s[i+j]<>w[j] then
          begin
            v[i]:=false;break;
          end;
    end;
  if q=0 then
    begin
      if v[0] then writeln(1) else writeln(0);
      exit;
    end;
  if q=1 then
    begin
      for i:=1 to n-1 do if v[i] then inc(ans);
      writeln(ans);exit;
    end;
  t:=0;t2:=1;
  for i:=2 to q do
    begin
      a:=t2*(n-1);
      a2:=t+t2*(n-2);
      t:=a mod mo;t2:=a2 mod mo;
    end;
  if v[0] then ans:=t;
  for i:=1 to n-1 do if v[i] then inc(ans,t2);
  writeln(ans mod mo);
end.

const
	md = round(1e9) + 7;

var
	i, k, l, g : longint;
	a, b : array [1 .. 1000] of char;
	dpa, dpb : array [0 .. 100000] of longint;

function eq(off : longint) : boolean;
var
	x, y, i : longint;
	bool : boolean;
begin
	bool := true;
	x := off;
	y := 0;
	for i := 1 to l do begin
		inc(x);
		if x > l then x := 1;
		inc(y);
		if y > l then y := 1;
		if a[x] <> b[y] then begin
			bool := false;
			break;
		end;
	end;
	exit(bool);
end;

begin
	while not eoln do begin inc(l); read(a[l]); end; readln;
	for i := 1 to l do read(b[i]);
	readln(k);
	for i := 0 to l - 1 do if eq(i) then inc(g);
	if eq(0) then dpa[0] := 1 else dpb[0] := 1;
	for i := 1 to k do begin
		dpa[i] := int64( dpa[i - 1] * int64(g - 1) + dpb[i - 1] * int64(g) ) mod md;
		dpb[i] := int64( dpa[i - 1] * int64(l - g) + dpb[i - 1] * int64(l - g - 1) ) mod md;
	end;
	writeln(dpa[k]);
end.

var
dp,c:array[0..1000]of longint;
i,j,n,m,k,answer,add:longint;
 s1,s2:ansistring;
 begin
  readln(s1);
  readln(s2);
  readln(k);

  if k=0 then
   begin
    if s1=s2 then writeln(1)
     else writeln(0);
    halt(0);
   end;

  if (length(s1)=2) then
   begin
      if k mod 2=0 then
       begin
        if s1=s2 then writeln(1)
         else writeln(0);
       end
      else
       begin
        s1:=s1+s1[1];
        delete(s1,1,1);
        if s1=s2 then writeln(1)
         else writeln(0);
       end;
      halt(0);
   end;

  dp[0]:=1;
  for i:=1 to k do
   begin
       fillchar(c,sizeof(c),0);
       add:=0;
       for j:=0 to length(s1)-1 do
         begin
          c[j]:=c[j]-dp[j];
          add:=(add+dp[j]) mod 1000000007;
         end;

       for j:=0 to length(s1)-1 do
        begin
         dp[j]:=(c[j]+add) mod 1000000007;
         dp[j]:=(dp[j]+1000000007) mod 1000000007;
        end;
   end;

  if s1=s2 then answer:=(answer+dp[0]) mod 1000000007;
  for i:=1 to length(s1)-1 do
   begin
    s1:=s1[length(s1)]+s1;
    delete(s1,length(s1),1);
    if s1=s2 then answer:=(answer+dp[i]) mod 1000000007;
   end;
  writeln(answer mod 1000000007);
 end.

const
{     fi='word.inp';
     fo='word.out';}
     fi='';
     fo='';
     inf=round(1e9)+7;
     maxk=100010;

var
     ff:text;
     a,b:ansistring;
     vt,k,i:longint;
     f,g:array[0..maxk]of int64;
     n,s:int64;
     ok:boolean;
     ch:char;

begin
     assign(ff,fi); reset(ff);
     readln(ff,a);
     readln(ff,b);
     n:=length(a);
     readln(ff,k);
     close(ff);

     ok:=false; s:=0;
     vt:=0;
     if a=b then vt:=1;
     for i:=1 to n do
     begin
          if a=b then
          begin
               ok:=true;
               inc(s);
          end;
          ch:=b[1];
          delete(b,1,1);
          b:=b+ch;
     end;

     assign(ff,fo); rewrite(ff);
     if s=0 then writeln(ff,0)
     else
     begin
          f[1]:=s-vt; g[1]:=n-1-s+vt;
          for i:=2 to k do
          begin
               f[i]:=(g[i-1]*s+f[i-1]*(s-1))mod inf;
               g[i]:=(f[i-1]*(n-s)+g[i-1]*(n-s-1)) mod inf;
          end;
          if (k=0) and (vt=1) then writeln(ff,1)
          else writeln(ff,f[k]);
     end;
     close(ff);
end.

{$H+}
uses math;
const
        tfi     =       '';
        tfo     =       '';
        nmax    =       101;
        p       =       1000000007;
var
        fi,fo:text;
        n,m,res,k:longint;
        S,T:string;
        f:Array[0..nmax,0..nmax] of longint;
        g:array[0..100001] of longint;
procedure mo;
var i,j:longint;
begin
        assign(fi,tfi);reset(fi);
        assign(fo,tfo);rewrite(fo);
end;
procedure nhap;
var i,j:longint;
begin
        readln(fi,S);
        readln(fi,T);
        read(fi,k);
        n:=length(S);
end;
function check(i:longint):boolean;
var j:longint;
begin
        for j:=i to n do
          if S[j]<>T[j-i+1] then exit(false);
        for j:=1 to i-1 do
          if S[j]<>T[(n-i+1)+j] then exit(false);
        exit(true);
end;
procedure khoitao;
var i,j:longint;
begin
end;
procedure xuly;
var i,j:longint;
begin
        g[0]:=0;
        for i:=1 to k do
          if i mod 2=0 then
            g[i]:=(g[i-1]*int64(n-1)-1)mod p
          else g[i]:=(g[i-1]*int64(n-1)+1)mod p;
        for i:=2 to n do
          if check(i) then
            res:=(res+g[k])mod p;
        if check(1) then
          begin
            if k mod 2=0 then res:=(res+g[k]+1)mod p
            else res:=(res+g[k]-1)mod  p;
          end;
        writeln(fo,(res+p)mod p);
end;
procedure inkq;
var i,j,t:longint;
begin
        f[0,1]:=1;
        for i:=1 to k do
          for j:=1 to n do
            for t:=1 to n do
              if j<>t then
                f[i,j]:=(f[i-1,t]+f[i,j])mod p;
        res:=0;
        for i:=1 to n do
          if check(i) then res:=(res+f[k,i])mod p;
        writeln(fo,(res+p)mod p);
end;
procedure dong;
var i,j:longint;
begin
        close(fi);
        close(fo);
end;
begin
        mo;
        nhap;
        khoitao;
        xuly;
      //  inkq;
        dong;
end.

const mm:longint=1000000007;

var s,v,w:ansistring;
    i,j,k,n,t,lt,ans:longint;
    a:array[0..1001] of longint;
 begin
  readln(s);
  readln(v);
  readln(k);
  n:=length(s);

  t:=1;
  a[n]:=1;
  for i:=1 to k do
   begin
    lt:=0;
    for j:=1 to n do
     begin
      a[j]:=(t-a[j]+mm) mod mm;
      lt:=(lt+a[j]) mod mm;
     end;
    t:=lt;
   end;

  ans:=0;
  for i:=1 to n do
   begin
    if i<n then w:=copy(v,i+1,n-i)+copy(v,1,i)
           else w:=v;

    if w=s then begin
                 ans:=(ans+a[i]) mod mm;
                end;
   end;

  writeln(ans);
 end.

const
 mo=1000000000+7;
var
 sum:array[0..100000]of int64;
 t:array[0..1000]of boolean;
 f:array[0..1000]of int64;
 ans:int64;
 a,b,st,en:ansistring;
 n,k,i:longint;
 found:boolean;

begin
 readln(st);
 readln(en);
 n:=length(st);
 readln(k);
 for i:=0 to n-1 do
  begin
   found:=true;
   if i>0 then
    begin
     a:=copy(st,1,i);
     b:=copy(en,n-i+1,i);
     if a<>b then found:=false;
    end;
   if i<(n-1) then
    begin
     a:=copy(st,i+1,(n-i));
     b:=copy(en,1,(n-i));
     if a<>b then found:=false;
    end;
   t[i]:=found;
  end;
 sum[1]:=n-1;
 for i:=2 to k do
  sum[i]:=(sum[i-1]*int64(n-1)) mod mo;
 ans:=0;
 for i:=1 to k-1 do
  ans:=(sum[i]+mo-ans)mod mo;
  if k<>0 then
  begin
 if k mod 2=0 then
 begin
  for i:=1 to n-1 do
   f[i]:=(ans+mo-1)mod mo;
  f[0]:=ans;
 end
else
  begin
  for i:=1 to n-1 do
   f[i]:=(ans+mo+1)mod mo;
  f[0]:=ans;
 end;
 end;
 ans:=0;
 for i:=0 to n-1 do
  if t[i] then ans:=(ans+f[i]) mod mo;
 if k<>0 then writeln(ans) else if t[0] then writeln('1') else writeln('0');
end.

program shablon;

{$MODE OBJFPC}{$H+}
{$O+ R-  Q-}
{$COperators ON}

uses
   SysUtils,
   Math;

const inf = 1 / 0;
      p = 1000 * 1000 * 1000 + 7;

var k  : longint;
    ans : int64;
    s, t : string;

procedure pout;
begin
    writeln(ans);
    {$IFNDEF ONLINE_JUDGE}
    close(input);
    close(output);
    {$ENDIF}
    halt(0);
end;

procedure pinit;
begin
    {$IFNDEF ONLINE_JUDGE}
    assign(input, 'input.txt');
    assign(output, 'output.txt');
    reset(input);
    rewrite(output);
    {$ENDIF}
    readln(s);
    readln(t);
    read(k);
end;

procedure solve;
var i, j, f1, f2, l : longint;
    r : string;
    flag : boolean;
    a1, a2, b1, b2 : qword;
begin
    if s = t then begin
        f1 := 1;
    end
    else begin
        f1 := 0;
    end;
    r := s + s;
    l := length(t);
    f2 := 0;
    for i := 2 to l do begin
        flag := true;
        for j := 1 to l do begin
            if r[i + j - 1] <> t[j] then begin
                flag := false;
                break;
            end;
        end;
        if flag then begin
            inc(f2);
        end;
    end;
    a1 := 1;
    a2 := 0;
    for i := 1 to k do begin
        b1 := (a2 * (l - 1)) mod p;
        b2 := (a2 * (l - 2) + a1) mod p;
        a1 := b1;
        a2 := b2;
    end;
    ans := (a1 * f1 + a2 * f2) mod p;
end;

begin
    pinit;
    solve;
    pout;
end.

uses math;
const debug=0;
      oo=high(longint);
      eps=1e-5;
      dx:array[0..7] of longint=(0,0,1,-1,-1,-1,1,1);
      dy:array[0..7] of longint=(-1,1,0,0,-1,1,-1,1);
      modd=round(1e9)+7;
var fi,fo:string;
    f,g:text;
    a,b:ansistring;
    ans,gt,k,n:int64;
    i:longint;
    s:array[1..2,0..100000] of int64;
    //-----------
begin
    randomize;
    fi:=''; fo:='';
    if debug=1 then begin fi:='i.inp'; fo:='o.out'; end;
    assign(f,fi); reset(F);
    assign(g,fo); rewrite(G);
    //
        readln(f,a); n:=length(a);
        readln(f,b);
        readln(f,k);
        if length(a)<>length(b) then begin writeln(g,0); close(f); close(g); halt; end;

        s[1,0]:=1;
        for i:=1 to k do begin
            s[1,i]:=((n-1)*s[2,i]+(n-2)*s[1,i]) mod modd;
            s[2,i]:=s[1,i-1];
        end;
        for i:=1 to n-1 do
         if copy(a,i+1,n-i)+copy(a,1,i)=b then ans:=(ans+s[2,k]) mod modd;

        s[1,0]:=0; s[2,0]:=1;
        for i:=1 to k do begin
            s[1,i]:=((n-1)*s[2,i]+(n-2)*s[1,i]) mod modd;
            s[2,i]:=s[1,i-1];
        end;
        if a=b then ans:=(ans+s[2,k]) mod modd;
        writeln(g,ans);
    close(g);
    close(f);
end.

const modulo = 1000000007;

var a, b , k, aa ,bb,ans :int64;
    st , fin : array [1..1009] of char;
    n:longint;

function equal( s : longint ): boolean;
var i,j:longint;
begin
  i:=s+1;
  if i > n then i:=1;
  j:=2;

  while (i<>s) and (st[j] = fin[i] ) do begin
    inc(j);
    inc(i);
    if i > n then i:=1;
  end;

  if (st[1] <> fin[i]) or (j<>n+1) then exit(false) else exit(true);
end;

var i :longint;
    c:char;
begin
  a:=1;
  b:=0;
  i:=0;

  while not EOLN do begin inc(i); read(st[i]); end;
  n:=i;
  i:=0;
  readln(c);
  for i:=1 to n do read(fin[i]);

  readln( k );

  ans:=0;
  for i:=1 to k do begin
    aa:=a; bb:=b;
    a:=bb*(n-1);
    b:=aa + bb*(n-2);
  end;

  //writeln(a,b);
  for i:=1 to n do begin
    if equal(i) then begin
      if i = 1 then inc(ans , a) else inc(ans , b);
    end;
    ans:= ans mod modulo;
  end;
  //writeln(equal(1));
  writeln( ans );

end.

const modulo = 1000000007;

var a, b , k, aa ,bb,ans :int64;
    st , fin : array [1..1009] of char;
    n:longint;

function equal( s : longint ): boolean;
var i,j:longint;
begin
  i:=s+1;
  if s+1 = n+1 then i:=1;
  j:=2;

  while (i<>s) and (st[j] <> fin[i] ) do begin
    inc(j);
    inc(i);
    if i > n then i:=1;
  end;

  if st[j] = fin[i] then exit(true) else exit(false);
end;

var i :longint;
    c:char;
begin
  a:=1;
  b:=0;
  i:=0;

  while not EOLN do begin inc(i); read(st[i]); end;
  n:=i;
  i:=0;
  readln(c);
  for i:=1 to n do read(fin[i]);

  readln( k );

  ans:=0;
  for i:=1 to k do begin
    aa:=a; bb:=b;
    a:=bb*(n-1);
    b:=aa + bb*(n-2);
  end;

  //writeln(a,b);
  for i:=1 to n do begin
    if equal(i) then begin
      if i = 1 then inc(ans , a) else inc(ans , b);
    end;
    ans:= ans mod modulo;
  end;

  writeln( ans );

end.

program qwe;
{$APPTYPE CONSOLE}
{$MAXSTACKSIZE $00200000}

uses
  SysUtils,
  Math;

//it is a big shablon. if you see any strange function, never mind

const base = 1000000007;
 eps = 1e-8;
 inf = 1 shl 30;

type
 TArray = array[0..10000010] of integer;
 int53 = int64;
 itneger = integer;
 point = record
  x, y: integer;
 end;

function sqrr(a, b: integer): int64;
var
 dega, res: int64;
begin
 res := 1;
 dega := a;
 while b > 0 do
 begin
  if odd(b) then res := res * dega;
  dega := dega * dega;
  b := b div 2;
  res := res mod base;
  dega := dega mod base;
 end;
 result := res mod base;
end;

function less(x, y: point): boolean;
begin
 if (x.x < y.x) or ((x.x = y.x) and (x.y < y.y)) then result := true else result := false;
end;

procedure swap(var x, y: integer);
var
 z: integer;
begin
 z := x;
 x := y;
 y := z;
end;

procedure swap64(var x, y: int64);
var
 z: int64;
begin
 z := x;
 x := y;
 y := z;
end;

procedure swapex(var x, y: extended);
var
 z: extended;
begin
 z := x;
 x := y;
 y := z;
end;

procedure swapp(var x, y: point);
var
 z: point;
begin
 z := x;
 x := y;
 y := z;
end;

var
 s, t: string;
 b: array[0..1010] of boolean;
 i, j, n, k: integer;
 x, y, ans: int64;

begin
 {$IFNDEF ONLINE_JUDGE}
 reset(input,'input.txt');
 rewrite(output, 'output.txt');
 {$ENDIF}
 readln(s);
 readln(t);
 n := length(s);
 for i := 1 to n do
 begin
  b[i - 1] := true;
  for j := 0 to n - 1 do
  b[i - 1] := b[i - 1] and (s[j + 1] = t[(i + j - 1) mod n + 1]);
 end;
 readln(k);
 ans := 0;
 if k = 0 then
 begin
  if s = t then
  writeln(1) else
  writeln(0);
  halt(0);
 end else
 if k = 1 then
 begin
  for i := 2 to n do
  if b[i - 1] then inc(ans);
  writeln(ans);
  halt(0);
 end;
 if n = 2 then
 begin
  if (s = t) and (s[1] = s[2]) then writeln(1) else
  if s = t then writeln(1 - k mod 2) else
  if (s[1] = t[2]) and (s[2] = t[1]) then writeln(k mod 2) else
  writeln(0);
  halt(0);
 end;
 dec(k, 2);
 x := n - 1;
 y := n - 2;
 for i := 1 to k do
 begin
  x := y * (n - 1) mod base;
  y := y * (n - 2) mod base;
  y := (y + x) mod base;
 end;
 for i := 1 to n - 1 do
 if b[i] then ans := (ans + y) mod base;
 if b[0] then ans := (ans + x) mod base;
 writeln(ans);
end.

program qwe;
{$APPTYPE CONSOLE}
{$MAXSTACKSIZE $00200000}

uses
  SysUtils,
  Math;

//it is a big shablon. if you see any strange function, never mind

const base = 1000000007;
 eps = 1e-8;
 inf = 1 shl 30;

type
 TArray = array[0..10000010] of integer;
 int53 = int64;
 itneger = integer;
 point = record
  x, y: integer;
 end;

function sqrr(a, b: integer): int64;
var
 dega, res: int64;
begin
 res := 1;
 dega := a;
 while b > 0 do
 begin
  if odd(b) then res := res * dega;
  dega := dega * dega;
  b := b div 2;
  res := res mod base;
  dega := dega mod base;
 end;
 result := res mod base;
end;

function less(x, y: point): boolean;
begin
 if (x.x < y.x) or ((x.x = y.x) and (x.y < y.y)) then result := true else result := false;
end;

procedure swap(var x, y: integer);
var
 z: integer;
begin
 z := x;
 x := y;
 y := z;
end;

procedure swap64(var x, y: int64);
var
 z: int64;
begin
 z := x;
 x := y;
 y := z;
end;

procedure swapex(var x, y: extended);
var
 z: extended;
begin
 z := x;
 x := y;
 y := z;
end;

procedure swapp(var x, y: point);
var
 z: point;
begin
 z := x;
 x := y;
 y := z;
end;

var
 s, t: string;
 b: array[0..1010] of boolean;
 i, j, n, k: integer;
 x, y, ans: int64;

begin
 {$IFNDEF ONLINE_JUDGE}
 reset(input,'input.txt');
 rewrite(output, 'output.txt');
 {$ENDIF}
 readln(s);
 readln(t);
 n := length(s);
 for i := 1 to n do
 begin
  b[i - 1] := true;
  for j := 0 to n - 1 do
  b[i - 1] := b[i - 1] and (s[j + 1] = t[(i + j - 1) mod n + 1]);
 end;
 readln(k);
 ans := 0;
 if k = 0 then
 begin
  writeln(1);
  halt(0);
 end else
 if k = 1 then
 begin
  for i := 2 to n do
  if b[i - 1] then inc(ans);
  writeln(ans);
  halt(0);
 end;
 if n = 2 then
 begin
  if (s = t) and (s[1] = s[2]) then writeln(1) else
  if s = t then writeln(1 - k mod 2) else
  if (s[1] = t[2]) and (s[2] = t[1]) then writeln(k mod 2) else
  writeln(0);
  halt(0);
 end;
 dec(k, 2);
 x := n - 1;
 y := n - 2;
 for i := 1 to k do
 begin
  x := y * (n - 1) mod base;
  y := y * (n - 2) mod base;
  y := (y + x) mod base;
 end;
 for i := 1 to n - 1 do
 if b[i] then ans := (ans + y) mod base;
 if b[0] then ans := (ans + x) mod base;
 writeln(ans);
end.

{$APPTYPE CONSOLE}

uses
  Math, SysUtils;

var
  f1, f2 : array [0.. 200000] of int64;
  a, b, s : string;
  i, k : Integer;
  ans : int64;

begin
  //reset(input, 'input.txt');
  //rewrite(output, 'output.txt');
  readln(a);
  readln(b);
  read(k);
  for i := 0 to k do begin
    f1[i] := 0;
    f2[i] := 0;
  end;

  f1[0] := 1;
  f1[1] := 1;
  f2[0] := 1;
  f2[1] := 0;
  for i := 2 to k do begin
    f1[i] := (f1[i - 1] * (length(a) - 2)) mod (1000000007);
    if i mod 2 = 0 then
      f2[i] := (f2[i - 2] * (Length(a) - 1)) mod 1000000007;
  end;

  for i := 1 to length(a) - 1 do begin
    s := copy(a, i + 1, length(a) - i) + copy(a, 1, i);
    if s = b then
      ans := ans + f1[k];
  end;

  if a = b then
    ans := ans + f2[k];

  write(ans);
end.

{$APPTYPE CONSOLE}

uses
  Math, SysUtils;

var
  f1, f2 : array [0.. 200000] of int64;
  a, b, s : string;
  i, k : Integer;
  ans : int64;

begin
  //reset(input, 'input.txt');
  //rewrite(output, 'output.txt');
  readln(a);
  readln(b);
  read(k);
  for i := 0 to k do begin
    f1[i] := 0;
    f2[i] := 0;
  end;

  f1[0] := 1;
  f1[1] := 1;
  f2[0] := 1;
  f2[1] := 0;
  for i := 2 to k do begin
    f1[i] := (f1[i - 1] * (length(a) - 2)) mod (1000000007);
    f2[i] := ( f1[i - 1] * (length(a) - 1)) mod 1000000007;
  end;

  ans := 0;
  for i := 1 to length(a) - 1 do begin
    s := copy(a, i + 1, length(a) - i) + copy(a, 1, i);
    if s = b then
      ans := ans + f1[k];
  end;

  if a = b then
    ans := ans + f2[k];

  write(ans);
end.

{$APPTYPE CONSOLE}

uses
  Math, SysUtils;

var
  f1, f2 : array [0.. 200000] of int64;
  a, b, s : string;
  i, k : Integer;
  ans : int64;

begin
  //reset(input, 'input.txt');
  //rewrite(output, 'output.txt');
  readln(a);
  readln(b);
  read(k);
  for i := 0 to k do begin
    f1[i] := 0;
    f2[i] := 0;
  end;

  f1[0] := 1;
  f1[1] := 1;
  f2[0] := 1;
  f2[1] := 0;
  for i := 2 to k do begin
    f1[i] := f1[i - 1] * (length(a) - 2) mod 1000000007;
    f2[i] := f1[i - 1] * (length(a) - 1) mod 1000000007;
  end;

  ans := 0;
  for i := 1 to length(a) - 1 do begin
    s := copy(a, i + 1, length(a) - i) + copy(a, 1, i);
    if s = b then
      ans := (ans + f1[k]) mod 1000000007;
  end;

  if a = b then
    ans := (ans + f2[k]) mod 1000000007;

  write(ans);
end.

{$APPTYPE CONSOLE}

uses
  Math, SysUtils;

var
  f1, f2 : array [0.. 200000] of int64;
  a, b, s : string;
  i, k : Integer;
  ans : int64;

begin
  //reset(input, 'input.txt');
  //rewrite(output, 'output.txt');
  readln(a);
  readln(b);
  read(k);
  for i := 0 to k do begin
    f1[i] := 0;
    f2[i] := 0;
  end;

  f1[0] := 1;
  f1[1] := 1;
  f2[0] := 1;
  f2[1] := 0;
  for i := 2 to k do begin
    f1[i] := (f1[i - 1] * (length(a) - 2) + f2[i - 1]) mod 1000000007;
    f2[i] := f1[i - 1] * (length(a) - 1) mod 1000000007;
  end;

  ans := 0;
  for i := 1 to length(a) - 1 do begin
    s := copy(a, i + 1, length(a) - i) + copy(a, 1, i);
    if s = b then
      ans := (ans + f1[k]) mod 1000000007;
  end;

  if a = b then
    ans := (ans + f2[k]) mod 1000000007;

  write(ans);
end.

program pr1;

{$APPTYPE CONSOLE}

     type double=int64;
const nn=1000001; modul=1000000007;
 eps=0.0000000001;  nnn=2000000;
 label 1,2,3,4;
var kol,j1,i,kk,pp,t,tt,k,i3,i0,i1,i2,p3,pgor,pver,step,j:longint;
p1,p,kolraz,rr,s1,mmin,imin,x0,y0,w,vp,vd,f,ii,n,ot,p2,besk,l,m,k1,k2,a2,jj:int64;
koef:double;  ch,ch1,ch2:char;
sdvig,vrag:array [-1..100000 ] of boolean;
x,y,gr:array[0..nn] of double;
s:double;
kol0,kol_0,dd:array[0..100000] of int64;
 oldmingor,oldminver,mingor,minver:array[0..1000] of longint;
sss,sss1,sss2,sss3: string;

a1,b1:array[0..200,0..200] of char;
aa:array[0..200,0..200] of boolean;
ss:array[0..30000] of string;
flag2,flag1,flag:boolean;
verot:double;
name:array[0..200] of string;
function min (a,b:longint):longint;begin min:=b;if a<b then min:=a;  end;
function max (a,b:longint):longint;begin max:=b;if a>b then max:=a;  end;

function nod(a,b:longint):longint; var p,t:longint;
begin if (a>0)and(b>0) then
   begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
    p:=a;
   end else if a=0 then p:=b else p:=a;
nod:=p;
end;

 function luck(a:longint):boolean;
 var i,j,k:longint;flag:boolean;
 c: array[0..10] of longint;
begin
  flag:=true;  j:=0;
  k:=a;
  while k>0 do begin inc(j); c[j]:=k mod 10; k:= k div 10; {writeln(j,' uu ',k); }  end;
 for i:=1 to j do if (c[i]<>4)and(c[i]<>7) then flag:=false;
 luck:=flag;
end;

  { function dist(i0,i1:longint):longint;
   var i,j,k,mmin,imin:longint; pr:array[0..3000] of boolean;
      dd:array[0..10000] of longint;
 begin  for i:=1 to n do pr[i]:=false;
   pr[i0]:=true;  dd[i0]:=0;
   for i:=1 to n do if not pr[i] then dd[i]:=gr[i0,i];
     k:=1;
       while k<n do
    begin
       inc(k);
        imin:=1; mmin:=1000000;
        for i:=1 to n do if (dd[i]<mmin) and (not pr[i]) then
          begin  imin:=i;mmin:=dd[i];   end;
       pr[imin]:=true;
       for i:=1 to n do if not pr[i] then dd[i]:=min(dd[i],dd[imin]+gr[imin,i]);

    end;

 dist:=dd[i1];

 end;  }

   function ttt(ss:string):longint; var i,j,k,s:longint; fl:boolean;
 begin k:=length(ss);
   s:=0; for i:=1 to k do s:=10*s+ord(ss[i])-48;
      ttt:=s;
 end;

         begin
{assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
}  readln(sss); readln(sss1); readln(kol);
    n:=length(sss);
    sss:=sss+sss; sss1:=sss1+sss1;
     { writeln(sss);  writeln(sss1);  writeln(n);
   } for i:=0 to n-1 do
      begin
         flag:=true;
           for j:=1 to n do if sss[i+j]<>sss1[j] then
           begin flag:=false; {writeln('j= ',j);} end;
         sdvig[i]:=flag;
      end;
     { for i:=0 to n-1 do write(sdvig[i],' '); writeln;
     }  kol0[1]:=0;kol_0[1]:=1;
     for i:=2 to kol do
       begin
          p:=n;
          kol0[i]:=((p-1)*kol_0[i-1]) mod modul;
          kol_0[i]:=((p-2)*kol_0[i-1]+kol0[i-1]) mod modul;
       end;
     { for i:=1 to kol do writeln(i,' ',kol0[i]:7,' ',kol_0[i]);
      writeln(kol0[kol],'   ==  ',kol_0[kol]);
     } p:=0;
       for i:=1 to n-1 do if sdvig[i] then inc(p);
        if sdvig[0] then p1:=1 else p1:=0;
      ot:=(p*kol_0[kol]+p1*kol0[kol]) mod modul;
                 writeln(ot);

   1:
            close(output);
end.

program pr1;

{$APPTYPE CONSOLE}

     type double=int64;
const nn=1000001; modul=1000000007;
 eps=0.0000000001;  nnn=2000000;
 label 1,2,3,4;
var kol,j1,i,kk,pp,t,tt,k,i3,i0,i1,i2,p3,pgor,pver,step,j:longint;
p,kolraz,rr,s1,mmin,imin,x0,y0,w,vp,vd,f,ii,n,ot,p1,p2,besk,l,m,k1,k2,a2,jj:int64;
koef:double;  ch,ch1,ch2:char;
sdvig,vrag:array [-1..100000 ] of boolean;
x,y,gr:array[0..nn] of double;
s:double;
kol0,kol_0,dd:array[0..100000] of int64;
 oldmingor,oldminver,mingor,minver:array[0..1000] of longint;
sss,sss1,sss2,sss3: string;

a1,b1:array[0..200,0..200] of char;
aa:array[0..200,0..200] of boolean;
ss:array[0..30000] of string;
flag2,flag1,flag:boolean;
verot:double;
name:array[0..200] of string;
function min (a,b:longint):longint;begin min:=b;if a<b then min:=a;  end;
function max (a,b:longint):longint;begin max:=b;if a>b then max:=a;  end;

function nod(a,b:longint):longint; var p,t:longint;
begin if (a>0)and(b>0) then
   begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
    p:=a;
   end else if a=0 then p:=b else p:=a;
nod:=p;
end;

 function luck(a:longint):boolean;
 var i,j,k:longint;flag:boolean;
 c: array[0..10] of longint;
begin
  flag:=true;  j:=0;
  k:=a;
  while k>0 do begin inc(j); c[j]:=k mod 10; k:= k div 10; {writeln(j,' uu ',k); }  end;
 for i:=1 to j do if (c[i]<>4)and(c[i]<>7) then flag:=false;
 luck:=flag;
end;

  { function dist(i0,i1:longint):longint;
   var i,j,k,mmin,imin:longint; pr:array[0..3000] of boolean;
      dd:array[0..10000] of longint;
 begin  for i:=1 to n do pr[i]:=false;
   pr[i0]:=true;  dd[i0]:=0;
   for i:=1 to n do if not pr[i] then dd[i]:=gr[i0,i];
     k:=1;
       while k<n do
    begin
       inc(k);
        imin:=1; mmin:=1000000;
        for i:=1 to n do if (dd[i]<mmin) and (not pr[i]) then
          begin  imin:=i;mmin:=dd[i];   end;
       pr[imin]:=true;
       for i:=1 to n do if not pr[i] then dd[i]:=min(dd[i],dd[imin]+gr[imin,i]);

    end;

 dist:=dd[i1];

 end;  }

   function ttt(ss:string):longint; var i,j,k,s:longint; fl:boolean;
 begin k:=length(ss);
   s:=0; for i:=1 to k do s:=10*s+ord(ss[i])-48;
      ttt:=s;
 end;

         begin
{assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
}  readln(sss); readln(sss1); readln(kol);   dec(kol);
    n:=length(sss);
    sss:=sss+sss; sss1:=sss1+sss1;
     { writeln(sss);  writeln(sss1);  writeln(n);
   } for i:=0 to n-1 do
      begin
         flag:=true;
           for j:=1 to n do if sss[i+j]<>sss1[j] then
           begin flag:=false; {writeln('j= ',j);} end;
         sdvig[i]:=flag;
      end;
      { for i:=0 to n-1 do write(sdvig[i],' ');
      } kol0[0]:=0;kol_0[0]:=1;
     for i:=1 to kol do
       begin
          p:=n;
          kol0[i]:=((p-1)*kol_0[i-1]) mod modul;
          kol_0[i]:=((p-2)*kol_0[i-1]+kol0[i-1]) mod modul;
       end;
      p:=0;
       for i:=1 to n-1 do if sdvig[i] then inc(p);
      ot:=(p*kol_0[kol]+kol0[0]) mod modul;
                    writeln(ot);

   1:
            close(output);
end.

program pr1;

{$APPTYPE CONSOLE}

     type double=int64;
const nn=1000001; modul=1000000007;
 eps=0.0000000001;  nnn=2000000;
 label 1,2,3,4;
var kol,j1,i,kk,pp,t,tt,k,i3,i0,i1,i2,p3,pgor,pver,step,j:longint;
p1,p,kolraz,rr,s1,mmin,imin,x0,y0,w,vp,vd,f,ii,n,ot,p2,besk,l,m,k1,k2,a2,jj:int64;
koef:double;  ch,ch1,ch2:char;
sdvig,vrag:array [-1..100000 ] of boolean;
x,y,gr:array[0..nn] of double;
s:double;
kol0,kol_0,dd:array[0..100000] of int64;
 oldmingor,oldminver,mingor,minver:array[0..1000] of longint;
sss,sss1,sss2,sss3: string;

a1,b1:array[0..200,0..200] of char;
aa:array[0..200,0..200] of boolean;
ss:array[0..30000] of string;
flag2,flag1,flag:boolean;
verot:double;
name:array[0..200] of string;
function min (a,b:longint):longint;begin min:=b;if a<b then min:=a;  end;
function max (a,b:longint):longint;begin max:=b;if a>b then max:=a;  end;

function nod(a,b:longint):longint; var p,t:longint;
begin if (a>0)and(b>0) then
   begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
    p:=a;
   end else if a=0 then p:=b else p:=a;
nod:=p;
end;

 function luck(a:longint):boolean;
 var i,j,k:longint;flag:boolean;
 c: array[0..10] of longint;
begin
  flag:=true;  j:=0;
  k:=a;
  while k>0 do begin inc(j); c[j]:=k mod 10; k:= k div 10; {writeln(j,' uu ',k); }  end;
 for i:=1 to j do if (c[i]<>4)and(c[i]<>7) then flag:=false;
 luck:=flag;
end;

  { function dist(i0,i1:longint):longint;
   var i,j,k,mmin,imin:longint; pr:array[0..3000] of boolean;
      dd:array[0..10000] of longint;
 begin  for i:=1 to n do pr[i]:=false;
   pr[i0]:=true;  dd[i0]:=0;
   for i:=1 to n do if not pr[i] then dd[i]:=gr[i0,i];
     k:=1;
       while k<n do
    begin
       inc(k);
        imin:=1; mmin:=1000000;
        for i:=1 to n do if (dd[i]<mmin) and (not pr[i]) then
          begin  imin:=i;mmin:=dd[i];   end;
       pr[imin]:=true;
       for i:=1 to n do if not pr[i] then dd[i]:=min(dd[i],dd[imin]+gr[imin,i]);

    end;

 dist:=dd[i1];

 end;  }

   function ttt(ss:string):longint; var i,j,k,s:longint; fl:boolean;
 begin k:=length(ss);
   s:=0; for i:=1 to k do s:=10*s+ord(ss[i])-48;
      ttt:=s;
 end;

         begin
{assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
}  readln(sss); readln(sss1); readln(kol);
   if kol=0 then begin writeln(1);goto 1;      end;
    n:=length(sss);
    sss:=sss+sss; sss1:=sss1+sss1;
     { writeln(sss);  writeln(sss1);  writeln(n);
   } for i:=0 to n-1 do
      begin
         flag:=true;
           for j:=1 to n do if sss[i+j]<>sss1[j] then
           begin flag:=false; {writeln('j= ',j);} end;
         sdvig[i]:=flag;
      end;
     { for i:=0 to n-1 do write(sdvig[i],' '); writeln;
     }  kol0[1]:=0;kol_0[1]:=1;
     for i:=2 to kol do
       begin
          p:=n;
          kol0[i]:=((p-1)*kol_0[i-1]) mod modul;
          kol_0[i]:=((p-2)*kol_0[i-1]+kol0[i-1]) mod modul;
       end;
     { for i:=1 to kol do writeln(i,' ',kol0[i]:7,' ',kol_0[i]);
      writeln(kol0[kol],'   ==  ',kol_0[kol]);
     } p:=0;
       for i:=1 to n-1 do if sdvig[i] then inc(p);
        if sdvig[0] then p1:=1 else p1:=0;
      ot:=(p*kol_0[kol]+p1*kol0[kol]) mod modul;
                 writeln(ot);

   1:
            close(output);
end.

{$R+,Q+,I+}
{$apptype console}
Uses SysUtils;
Var
   s1,s2:String;
   pref,suf:Array[0..3000] of String;
   i,md:Integer;
   k,t,r,r1,r2,ans,n:Int64;
Begin
   md:=1000000007;
   ReadLn(s1);
   ReadLn(s2);
   Read(k);
   If k=0 Then Begin
      If s1=s2 Then Write(1)
               Else Write(0);
   End;
   r1:=0;
   r2:=0;
   n:=length(s1);
   pref[1]:=s1[1];
   For i:=2 To n Do pref[i]:=pref[i-1]+s1[i];
   suf[n]:=s1[n];
   For i:=n-1 DownTo 1 Do suf[i]:=s1[i]+suf[i+1];
   If s1=s2 Then r1:=1;
   For i:=2 To n Do
      If suf[i]+pref[i-1]=s2 Then r2:=r2+1;
   If k=1 Then Begin
      Write(r2);
      Halt;
   End;
   t:=1;
   r:=1;
   For i:=1 To k Do Begin
      t:=(md+r-t) mod md;
      r:=(r*(n-1)) mod md;
   End;
   ans:=t*r1 mod md;
   t:=0;
   r:=1;
   For i:=1 To k Do Begin
      t:=(md+r-t) mod md;
      r:=(r*(n-1)) mod md;
   End;
   ans:=(ans+t*r2) mod md;
   Write(ans);
End.

program cf176b;
const mo=1000000007;
var
  n,m,i,j,kk:longint;
  jz:array[0..3000]of int64;
  s1,s2:array[0..3000]of char;
  f0,h1,h2,f1:array[0..200000]of int64;
  ct0,ct1:int64;
  ans:int64;
begin
  jz[0]:=1;
  for i:=1 to 3000 do jz[i]:=jz[i-1]*29;

  n:=0;
  while not(eoln) do begin
    inc(n);
    read(s1[n]);
  end;
  readln;
  m:=0;
  while not(eoln) do begin
    inc(m);
    read(s2[m]);
  end;     readln;
  if n<>m then begin
    writeln(0);
    halt;
  end;

  for i:=m+1 to 2*m do s2[i]:=s2[i-m];

  h2[0]:=0;
  for i:=1 to 2*m do h2[i]:=(h2[i-1]*29+(ord(s2[i])-ord('a')+1) );
  h1[0]:=0;
  for i:=1 to n do h1[i]:=(h1[i-1]*29+(ord(s1[i])-ord('a')+1) );
  if h2[m]=h1[n] then ct0:=1 else ct0:=0;

  ct1:=0;
  for i:=m+1 to 2*m-1 do
    if h2[i]-(h2[i-m]*jz[m])=h1[m] then inc(ct1);

  f0[0]:=1;
  f1[0]:=0;
  readln(kk);
  for i:=1 to kk do begin
    f0[i]:=f1[i-1]*(n-1) mod mo;
    f1[i]:=f0[i-1];
  end;

  if ct0=1 then ans:=f0[kk] else ans:=0;
  ans:=((ans+f1[kk]*ct1)mod mo);
  writeln(ans);
end.

program cf176b;
const mo=1000000007;
var
  n,m,i,j,kk:longint;
  jz:array[0..3000]of int64;
  s1,s2:array[0..3000]of char;
  f0,h1,h2,f1:array[0..200000]of int64;
  ct0,ct1:int64;
  ans:int64;
begin
  jz[0]:=1;
  for i:=1 to 7 do jz[i]:=jz[i-1]*29;

  n:=0;
  while not(eoln) do begin
    inc(n);
    read(s1[n]);
  end;
  readln;
  m:=0;
  while not(eoln) do begin
    inc(m);
    read(s2[m]);
  end;     readln;
  if n<>m then begin
    writeln(0);
    halt;
  end;

  for i:=m+1 to 2*m do s2[i]:=s2[i-m];

  h2[0]:=0;
  for i:=1 to 2*m do h2[i]:=(h2[i-1]*29+(ord(s2[i])-ord('a')+1) );
  h1[0]:=0;
  for i:=1 to n do h1[i]:=(h1[i-1]*29+(ord(s1[i])-ord('a')+1) );
  if h2[m]=h1[n] then ct0:=1 else ct0:=0;

  ct1:=0;
  for i:=m+1 to 2*m-1 do
    if h2[i]-(h2[i-m]*jz[m])=h1[m] then inc(ct1);

  f0[0]:=1;
  f1[0]:=0;
  readln(kk);
  for i:=1 to kk do begin
    f0[i]:=f1[i-1]*(n-1) mod mo;
    f1[i]:=f0[i-1];
  end;

  if ct0=1 then ans:=f0[kk] else ans:=0;
  ans:=((ans+f1[kk]*ct1)mod mo);
  writeln(ans);
end.

program cf176b;
const mo=1000000007;
var
  n,m,i,j,kk:longint;
  jz:array[0..3000]of int64;
  s1,s2:array[0..3000]of char;
  f0,h1,h2,f1:array[0..200000]of int64;
  ct0,ct1:int64;
  ans:int64;
begin
  jz[0]:=1;
  for i:=1 to 7 do jz[i]:=jz[i-1]*29;

  n:=0;
  while not(eoln) do begin
    inc(n);
    read(s1[n]);
  end;
  readln;
  m:=0;
  while not(eoln) do begin
    inc(m);
    read(s2[m]);
  end;     readln;
  if n<>m then begin
    writeln(0);
    halt;
  end;

  for i:=m+1 to 2*m do s2[i]:=s2[i-m];

  h2[0]:=0;
  for i:=1 to 2*m do h2[i]:=(h2[i-1]*29+(ord(s2[i])-ord('a')+1) );
  h1[0]:=0;
  for i:=1 to n do h1[i]:=(h1[i-1]*29+(ord(s1[i])-ord('a')+1) );
  if h2[m]=h1[n] then ct0:=1 else ct0:=0;

  ct1:=0;
  for i:=m+1 to 2*m-1 do
    if h2[i]-(h2[i-m]*jz[m])=h1[m] then inc(ct1);

  f0[0]:=1;
  f1[0]:=0;
  readln(kk);
  for i:=1 to kk do begin
    f0[i]:=f1[i-1]*(n-1) mod mo;
    f1[i]:=f0[i-1];
  end;

  if ct0=1 then ans:=f0[kk] else ans:=0;
  ans:=(ans+f1[kk]*ct1);
  writeln(ans);
end.

{$Mode objfpc}

const
    maxN= 1000;
    base= 1000000007;

var
    s, t: ansistring;
    id: array[1.. maxN] of integer;
    a: array[1.. maxN] of ansistring;
    i, j, m, n, k: integer;
    res: int64;

procedure sort(l, h: integer);
var
    i, j, p: integer;
begin
    if l>= h then exit;
    i:= random(h- l+ 1)+ l;
    p:= id[i];
    id[i]:= id[l];
    i:= l; j:= h;
    repeat
        while (i< j) and (a[id[j]]> a[p]) do dec(j);
        if i< j then
            begin
                id[i]:= id[j];
                inc(i);
            end else break;
        while (i< j) and (a[id[i]]< a[p]) do inc(i);
        if i< j then
             begin
                 id[j]:= id[i];
                 dec(j);
             end else break;
    until i= j;
    id[i]:= p;
    sort(l, i- 1);
    sort(i+ 1, h);
end;

begin
//    assign(input, 'text.inp'); reset(input);

    readln(s);
    readln(t);
    readln(k);

    n:= length(s);
    for i:= 1 to n do
        begin
            a[i]:= s;
            s:= s+ s[1];
            delete(s, 1, 1);
        end;
    for i:= 1 to n do id[i]:= i;
    sort(1, n);
    m:= 0;
    for i:= 1 to n do if t= a[id[i]] then
        begin
            j:= i;
            while (j<= n) and (t= a[id[j]]) do inc(j);
            m:= j- i;
            break;
        end;
    if m= 0 then
        begin
            writeln(0);
            halt;
        end;
    res:= 1;
    for i:= 1 to k- 2 do res:= res* (n- 1) mod base;
    if k> 1 then
        begin
            if (k= 2) and (s= t) then res:= res* (n- 1) mod base else
            res:= res* (n- 2) mod base;
        end;
    if s<> t then res:= res* m mod base else
        if (k= 1) then res:= res* (m- 1) mod base;
    writeln(res);
end.

{$Mode objfpc}
{$M 20000000}
{$H+}

const
    maxN= 100000;
    base= round(1e9)+ 7;

var
    f, g: array[0.. maxn] of int64;
    i, j, k, n, d: integer;
    res: int64;
    s, t, x: string;

begin
  ///  assign(input, 'test.inp'); reset(input);

    readln(s);
    readln(t);
    n:= length(t);
    readln(k);
    x:= s;
    for i:= 2 to n do
        begin
            x:= x+ x[1];
            delete(x, 1, 1);
            if x= t then inc(d);
        end;
    g[0]:= 1;
    f[1]:= 1;
    g[1]:= 0;
    for i:= 2 to k do
        begin
            f[i]:= ( (n- 2)* f[i- 1]+ (n- 1)* g[i- 1]) mod base;
            g[i]:= f[i- 1]* (n- 1) mod base;
        end;
    res:= f[k]* d mod base;
    if s= t then res:= (res+ g[k]) mod base;
    writeln(res);
end.

{$Mode objfpc}
{$M 20000000}
{$H+}

const
    maxN= 100000;
    base= round(1e9)+ 7;

var
    f, g: array[1.. maxn] of int64;
    i, j, k, n, d: integer;
    res: int64;
    s, t, x: string;

begin
  ///  assign(input, 'test.inp'); reset(input);

    readln(s);
    readln(t);
    n:= length(t);
    readln(k);
    x:= s;
    for i:= 2 to n do
        begin
            x:= x+ x[1];
            delete(x, 1, 1);
            if x= t then inc(d);
        end;
    f[1]:= 1;
    g[1]:= 0;
    for i:= 2 to k do
        begin
            f[i]:= ( (n- 2)* f[i- 1]+ (n- 1)* g[i- 1]) mod base;
            g[i]:= f[i- 1]* (n- 1) mod base;
        end;
    res:= f[k]* d mod base;
    if s= t then res:= (res+ g[k]) mod base;
    writeln(res);
end.

{$Mode objfpc}
{$M 20000000}
{$H+}

const
    maxN= 100000;
    base= round(1e9)+ 7;

var
    f, g: array[0.. maxn] of int64;
    i, j, k, n, d: integer;
    res: int64;
    s, t, x: string;

begin
  ///  assign(input, 'test.inp'); reset(input);

    readln(s);
    readln(t);
    n:= length(t);
    readln(k);
    x:= s;
    for i:= 2 to n do
        begin
            x:= x+ x[1];
            delete(x, 1, 1);
            if x= t then inc(d);
        end;
    f[1]:= 1;
    g[1]:= 0;
    for i:= 2 to k do
        begin
            f[i]:= ( (n- 2)* f[i- 1]+ (n- 1)* g[i- 1]) mod base;
            g[i]:= f[i- 1]* (n- 1) mod base;
        end;
    res:= f[k]* d mod base;
    if s= t then res:= (res+ g[k]) mod base;
    writeln(res);
end.

var           i,j,n,m,k,answer:longint;
 s1,s2:ansistring;
 begin
  readln(s1);
  readln(s2);
  readln(k);

  if k=0 then
   begin
    if s1=s2 then writeln(1)
     else writeln(0);
    halt(0);
   end;

  if k>1 then
   begin
    if s1=s2 then answer:=1;
   end;

  for i:=1 to length(s1)-1 do
   begin
     s1:=s1[length(s1)]+s1;
     delete(s1,length(s1),1);
     if s1=s2 then
      begin
       if length(s1)=2 then
        begin
         if k mod 2=1 then answer:=answer+1;
        end
       else
       answer:=answer+1;
      end;
   end;

  writeln(answer);
 end.

const
{     fi='word.inp';
     fo='word.out';}
     fi='';
     fo='';
     inf=round(1e9)+7;
     maxk=100010;

var
     ff:text;
     a,b:ansistring;
     vt,k,i:longint;
     f,g:array[0..maxk]of int64;
     n,s:int64;
     ok:boolean;
     ch:char;

begin
     assign(ff,fi); reset(ff);
     readln(ff,a);
     readln(ff,b);
     n:=length(a);
     readln(ff,k);
     close(ff);

     ok:=false; s:=0;
     vt:=0;
     if a=b then vt:=1;
     for i:=1 to n do
     begin
          if a=b then
          begin
               ok:=true;
               inc(s);
          end;
          ch:=b[1];
          delete(b,1,1);
          b:=b+ch;
     end;

     assign(ff,fo); rewrite(ff);
     if s=0 then writeln(ff,0)
     else
     begin
          f[1]:=s-vt; g[1]:=n-1-s+vt;
          for i:=2 to k do
          begin
               f[i]:=(g[i-1]*s+f[i-1]*(s-1))mod inf;
               g[i]:=(f[i-1]*(n-s)+g[i-1]*(n-s-1)) mod inf;
          end;
          writeln(ff,f[k]);
     end;
     close(ff);
end.

const
 mo=1000000000+7;
var
 sum:array[0..100000]of int64;
 t:array[0..1000]of boolean;
 f:array[0..1000]of int64;
 ans:int64;
 a,b,st,en:ansistring;
 n,k,i:longint;
 found:boolean;

begin
 readln(st);
 readln(en);
 n:=length(st);
 readln(k);
 for i:=0 to n-1 do
  begin
   found:=true;
   if i>0 then
    begin
     a:=copy(st,1,i);
     b:=copy(en,n-i+1,i);
     if a<>b then found:=false;
    end;
   if i<(n-1) then
    begin
     a:=copy(st,i+1,(n-i));
     b:=copy(en,1,(n-i));
     if a<>b then found:=false;
    end;
   t[i]:=found;
  end;
 sum[1]:=n-1;
 for i:=2 to k do
  sum[i]:=(sum[i-1]*int64(n-1)) mod mo;
 ans:=0;
 for i:=1 to k-1 do
  ans:=(sum[i]+mo-ans)mod mo;
 if k mod 2=0 then
 begin
  for i:=1 to n-1 do
   f[i]:=(ans+mo-1)mod mo;
  f[0]:=ans;
 end
else
  begin
  for i:=1 to n-1 do
   f[i]:=(ans+mo+1)mod mo;
  f[0]:=ans;
 end;
 ans:=0;
 for i:=0 to n-1 do
  if t[i] then ans:=(ans+f[i]) mod mo;
 writeln(ans);
end.


Limak is a little polar bear. Polar bears hate long strings and thus they like to compress them. You should also know that Limak is so young that he knows only first six letters of the English alphabet: 'a', 'b', 'c', 'd', 'e' and 'f'.You are given a set of q possible operations. Limak can perform them in any order, any operation may be applied any number of times. The i-th operation is described by a string ai of length two and a string bi of length one. No two of q possible operations have the same string ai.When Limak has a string s he can perform the i-th operation on s if the first two letters of s match a two-letter string ai. Performing the i-th operation removes first two letters of s and inserts there a string bi. See the notes section for further clarification.You may note that performing an operation decreases the length of a string s exactly by 1. Also, for some sets of operations there may be a string that cannot be compressed any further, because the first two letters don't match any ai.Limak wants to start with a string of length n and perform n - 1 operations to finally get a one-letter string "a". In how many ways can he choose the starting string to be able to get "a"? Remember that Limak can use only letters he knows.

The first line contains two integers n and q (2 ≤ n ≤ 6, 1 ≤ q ≤ 36) — the length of the initial string and the number of available operations. The next q lines describe the possible operations. The i-th of them contains two strings ai and bi (|ai| = 2, |bi| = 1). It's guaranteed that ai ≠ aj for i ≠ j and that all ai and bi consist of only first six lowercase English letters.

Print the number of strings of length n that Limak will be able to transform to string "a" by applying only operations given in the input.

2 8
af e
dc d
cc f
bc b
da b
eb a
bb b
ff c

NoteIn the first sample, we count initial strings of length 3 from which Limak can get a required string "a". There are 4 such strings: "abb", "cab", "cca", "eea". The first one Limak can compress using operation 1 two times (changing "ab" to a single "a"). The first operation would change "abb" to "ab" and the second operation would change "ab" to "a".Other three strings may be compressed as follows:   "cab"  "ab"  "a"  "cca"  "ca"  "a"  "eea"  "ca"  "a" In the second sample, the only correct initial string is "eb" because it can be immediately compressed to "a".

var
n, q, kol, i: integer;
pol: int64;
s: array[1..100] of string;

procedure res(k: integer; prev: char);
var
i: integer;
begin
kol:= 0;
if k = n - 1 then
begin
for i:= 2 to q + 1 do
if s[i][4] = prev then inc(kol);
pol:= pol + kol;
end
else
for i:= 2 to q + 1 do
if s[i][4] = prev then res(k + 1, s[i][1]);
end;

begin
read(n, q);
for i:= 1 to q + 1 do
readln(s[i]);
s[1]:= 'a';
res(1, 'a');
writeln(pol);
end.

label 1,2;
var step,dloch,newdl,i1,j1,k,k1,m,i,j,n:longint; q,pos,koef,ot,t,t1,t2,tt,p3,p4,p1,p2,p,rr,xx,yy,hh:int64;
flag1,flag2,flag:boolean;
newoch,och,r,c,z,a,b,x,y:array[-1000000..10000000] of int64;
marked:array[-1000..20000000] of boolean;
aa,bb,cc:extended;
ch1,ch2,ch:char;
sss:string;
ss,ss1,ss2: array [-10..100]of  string;
npole,pole:array[-10..200,-10..200] of char;
gr,rrr: array[-100..300,-100..300] of int64;
function max(a,b:int64):int64; begin max:=a; if b>a then max:=b;  end;
function min(a,b:int64):int64; begin min:=a; if b<a then min:=b;  end;

function arcsin(x:extended):extended;
 begin
    if x>=1 then arcsin:=pi/2 else arcsin:=arctan(x/sqrt(1-x*x));

 end;
  function phi(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     phi:=p;
 end;

 function right(ii:longint):extended; var al,t,p:extended; i:longint;
  begin  al:=phi(x[ii],y[ii]); t:=x[ii]*x[ii]+y[ii]*y[ii]; t:=sqrt(t);  right:=al+arcsin(t/rr);  end;

function nod(a,b:int64):int64; var p,t:int64;
begin if (a>0)and(b>0) then
   begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
    p:=a;
   end else if a=0 then p:=b else p:=a;
nod:=p;
end;

function zam(s:string):string;  label 66; var st,sot:string; ii,i,j:longint;
begin
  st:=s[1]+s[2];
  j:=length(s);  ii:=-1;
  for i:=1 to m do
    begin
      { writeln(i,' ',sot,'-',ss[i]);
      } if ss[i]=st then begin sot:=ss1[i]; ii:=i;  end;

    end;
   { writeln('sot= ',sot,'ii= ',ii);
   }
    if ii<0 then sot:='zzzz';
   for i:=3 to j do sot:=sot+s[i];

zam:=sot;
end;

function good(s:string):boolean; var s1,s2:string; step,i,j,k:longint; fl:boolean;
begin
  k:=length(s);
  s1:=s; fl:=true;
   for  step:=1 to k-1 do begin s1:=zam(s1);
   { writeln(s1); }   end;

    if s1='a' then good:=true else good:=false;

end;

begin

  { assign(input,'E:/Projects/input.txt');reset(input);
   assign(output,'E:/Projects/output.txt');rewrite(output);
   }

  readln(n,m);
  for i:=1 to m do begin read(ch1,ch2); ss[i]:=ss[i]+ch1+ch2; read(ch); readln(ch); ss1[i]:=ch;   end;
  {for i:=1 to m do writeln(ss[i],'+',ss1[i],'==');
  } t:=1 ; for i:=1 to n do t:=t*6;
    for step:=0 to t-1 do
      begin
          sss:='';
         q:=step;
         for i:=1 to n do begin a[i]:=q mod 6; q:=q div 6; end;
         for i:=1 to n do sss:=sss+chr(a[i]+ord('a'));

         if good(sss) then begin {writeln(sss);} inc(ot);  end;

      end;

    writeln(ot);

  {  writeln()
   }

1:
end.

Var
  n,i,q,ans:longint;
  c:char;
  a,b:array[1..36]of string;
  s:string;
Function prov(s:string):boolean;
Var
  i,j:longint;
  st:string;
Begin
  for i:=1 to 5 do
    begin
      st:=copy(s,1,2);
      for j:=1 to q do
        if st=a[j] then
          begin
            delete(s,1,2);
            s:=b[j]+s;
            break;
          end;
    end;
  prov:=s='a';
end;
Procedure pereb(s:string;tk:longint);
Var
  c:char;
begin
  if tk=n+1 then
    begin
      if prov(s) then
        inc(ans);
    end
  else
    for c:='a' to 'f' do
      pereb(s+c,tk+1);
end;
begin
//  assign(input,'input.txt');assign(output,'output.txt');
  readln(n,q);
  for i:=1 to q do
    begin
      read(c);
      a[i]:=c;
      read(c);
      a[i]:=a[i]+c;
      read(c);
      read(c);
      b[i]:=c;
      readln;
    end;
  Pereb('',1);
  writeln(ans);
end.

var a,b:array [1..100] of longint;
q,n,i,k:longint;
s:string;
procedure dfs(x,y:longint);
var j:longint;
 begin
  for j:=1 to q do
    if (y=n) and (b[j]=x) then inc(k) else
    if b[j]=x then dfs(a[j],y+1);
 end;
begin
readln(n,q);
for i:=1 to q do
 begin
  readln (s);
  a[i]:=ord(s[1])-96;
  b[i]:=ord(s[4])-96;
 end;
dfs(1,2);
write (k);
end.

Const Finp='';
      Fout='';
      maxCase=46656;
Var n:longint;
    change:array['a'..'f','a'..'f'] of string;
    hd:array[1..maxcase] of string;

Procedure Nhap;
Var f:Text;
    i,q:longint;
    a,b,s:string;
Begin
    assign(f,finp); reset(f);
    readln(f,n,q);
    for i:=1 to q do
        begin
            readln(f,s);
            a:=s[1]+s[2]; b:=s[4];
            if (a[1]<='f') or (a[2]<='f') or (b<='f') then
                change[a[1],a[2]]:=b;
        end;
    close(f);
End;
Function DecToSen(x:longint):string;
Var s:string;
Begin
    s:='';
    while x>0 do
        begin
            s:=chr((x mod 6)+97)+s;
            x:=x div 6;
        end;
    dectosen:=s;
End;
Function Check(s:string):boolean;
Begin
    while length(s)>1 do
        begin
            if change[s[1],s[2]]='' then
                exit(false);
            s:=change[s[1],s[2]]+s;
            delete(s,2,2);
        end;
    if s='a' then
        exit(true)
    else
        exit(false);
End;
Function Dem:longint;
Var i,d:longint;
    s:string;
Begin
    d:=0;
    for i:=0 to maxcase do
        begin
            s:=dectosen(i);
            if length(s)>n then
                break;
            while length(s)<n do
                s:='a'+s;
            if change[s[1],s[2]]='' then
                continue;
            if check(s)=true then
                inc(d);
        end;
    dem:=d;
End;
Procedure Xuat;
Var f:Text;
Begin
    assign(f,fout); rewrite(f);
    writeln(f,dem);
    close(f);
End;

Begin
    nhap;
    xuat;
End.

var n,m,ansv:int64;
A:array [1..100] of string;
s:string;
i,i1,i2,i3,i4,i5,i6:longint;
function canBuild(s:string):boolean;
var i,j:longint;
flag:boolean;
begin
 flag:=true;
while (flag=true) and (length(s)>1) do
begin
flag:=false;
  for i:=1 to m do
  if (A[i][1]=s[1]) and (A[i][2]=s[2]) then begin
                                            s[2]:=A[i][4];
                                            delete(s,1,1);
                                            flag:=true;
                                            break;
                                            end;
end;
if s='a' then canBuild:=true else canBuild:=false;
end;
begin
{assign(input,'input.txt');
assign(output,'output.txt');
reset(input);
rewrite(output); }
readln(n,m);

for i:=1 to m do
readln(A[i]);

for i:=1 to n do
s:=s+'a';

for i1:=1 to 6 do
begin
s[1]:=chr(96+i1);
  for i2:=1 to 6 do
  begin
  s[2]:=chr(96+i2);
  if n=2 then if canBuild(s) then inc(ansv);
    for i3:=1 to 6 do
    begin
    if n<3 then break;
    s[3]:=chr(96+i3);
    if n=3 then if canBuild(s) then inc(ansv);
      for i4:=1 to 6 do
      begin
      if n<4 then break;
      s[4]:=chr(96+i4);
      if n=4 then if canBuild(s) then inc(ansv);
        for i5:=1 to 6 do
        begin
        if n<5 then break;
        s[5]:=chr(96+i5);
        if n=5 then if canBuild(s) then inc(ansv);
          for i6:=1 to 6 do
          begin
          if n<6 then break;
          s[6]:=chr(96+i6);
          if n=6 then if canBuild(s) then inc(ansv);
          end;
        end;
      end;
    end;
  end;
end;

writeln(ansv);
{close(input);
close(output);  }
end.

var
a,b:array[1..50000] of string;
p,q:array[1..40] of string;
t:array[1..10] of longint;
s,s1:string;
n,l,i,j,j1:longint;
begin
read(n); readln(l);
t[1]:=1;
b[1]:='a';
for i:=1 to l do
begin
readln(s);
p[i]:=copy(s,1,2);
q[i]:=copy(s,4,1);
end;
for i:=1 to n-1 do
begin
        for j:=1 to t[i] do
                for j1:=1 to l do
                begin
                s:=b[j];
                if(s=q[j1]) then
                begin
                inc(t[i+1]);
                s1:=p[j1];
                a[t[i+1]]:=s1[1];
                end;
                end;
for j:=1 to t[i] do b[j]:='';
for j:=1 to t[i+1] do b[j]:=a[j];
end;
writeln(t[n]);
end.

VAR A:ARRAY[0..40]OF string;N,i,k:longint;z,y,x:array['a'..'f']of int64;
c:array['a'..'f','a'..'f']of int64;q,w:char;t:int64;
begin
 readln(n,k);
 for i:=1 to k do
 begin
  readln(a[i]);

  inc(c[a[i][4],a[i][1]]);
 end;
 x['a']:=1;
 for i:=1 to n-1 do
 begin
 for q:='a' to 'f' do
 for w:='a' to 'f' do
 y[q]:=y[q]+c[w,q]*x[w];
 x:=y;y:=z;
 end;
 t:=0;
 for q:='a' to 'f' do
 t:=t+x[q];
 write(t);
end.

var s,s1:string;
    way:array[1..8,1..37]of string;
    num:array[1..8]of longint;
    get:array[1..500000]of string;
    ans,m,n,i,a,tot:longint;

procedure dfs(s:string);
var s1,s2:string;
    i,j:longint;
begin
    s1:=s;
    if length(s)=n then begin
        inc(tot);
        get[tot]:=s;
        exit;
    end;
    for i:=1 to 1 do begin
        for j:=1 to num[ord(s[i])-96] do begin
            s2:=way[ord(s[i])-96,j];
            delete(s1,i,1);
            insert(s2,s1,i);
            dfs(s1);
            s1:=s;
        end;
    end;
end;

procedure qsort(l,r:longint);
var i,j:longint;
    mid,t:string;
begin
    i:=l;
    j:=r;
    mid:=get[(i+j)div 2];
    repeat
       while get[i]<mid do inc(i);
       while get[j]>mid do dec(j);
       if i<=j then begin
           t:=get[i];
           get[i]:=get[j];
           get[j]:=t;
           inc(i);
           dec(j);
       end;
    until i>j;
    if i<r then qsort(i,r);
    if l<j then qsort(l,j);
end;

begin
    readln(n,m);
    for i:=1 to m do begin
        readln(s);
        a:=ord(s[4])-96;
        inc(num[a]);
        s1:=copy(s,1,2);
        way[a,num[a]]:=s1;
    end;
    dfs('a');
    if tot>1 then qsort(1,tot);
    if tot>0 then ans:=1;
    for i:=2 to tot do if get[i]<>get[i-1] then inc(ans);
    writeln(ans);
end.

var
 i,j,m,n,x,y,s,t,k,h,p,tot:longint;
 s1,s2,s3:string;
 t1,t2:array[1..100] of string;
 vis,ans:array[0..1000000] of longint;
 q:array[0..1000000] of longint;
begin
 readln(n,m);
 for i:=1 to m do
 begin
  readln(s1);
  for j:=1 to length(s1) do
  if (s1[j]=' ') then break;
  s2:=copy(s1,1,j-1);
  t1[i]:=s2;
  s2:=copy(s1,j+1,1);
  t2[i]:=s2;
 end;
 h:=0; t:=1;
 q[1]:=1; vis[1]:=1; ans[1]:=1;
 while (h<t) do
  begin
    inc(h);
    x:=q[h];
    if (ans[h]>=n) then break;
    s3:='';
    while (x>=0) do
    begin
      k:=x mod 7;
      s3:=chr(k+96)+s3;
      x:=x div 7;
      if x=0 then break;
    end;
    for i:=1 to m do
       if (s3[1]=t2[i]) then
       begin
        s2:=t1[i];
        s1:=s3;
        delete(s1,1,1);
        s2:=s2+s1;
        x:=0;   p:=1;
        for k:=length(s2) downto 1 do
         begin
           x:=x+p*(ord(s2[k])-96);
           p:=p*7;
         end;
        if (vis[x]=0)  then
        begin
          vis[x]:=1;
          inc(t);
          ans[t]:=ans[h]+1;
          q[t]:=x;
        end;
       end;
  end;
  for i:=1 to t do
  if ans[i]=n then inc(tot);
  writeln(tot);
end.

Program GIO;

var a,b:array[1..36]of char;
c:array[0..500000]of char;
i,q,qq,n,j,k,f,t:longint;
s:string;
begin

readln(q,n);

for i:=1 to n do
begin
readln(s);
b[i]:=s[1];
a[i]:=s[4];
end;

f:=1;
t:=1;
c[1]:='a';

for i:=1 to q-1 do
begin
qq:=0;
for k:=f to t do
for j:=1 to n do
if (c[k]=a[j]) then begin qq:=qq+1; c[t+qq]:=b[j] end;
f:=t+1; t:=t+qq;
end;

writeln(qq);

end.

const fin='';
      fout='';
type weird=record
    c,s1,s2:char;
end;
var b:array[1..36] of weird;
    n,q:longint;
    f,g:text;
procedure readdata;
var i,j:longint;
    t:char;
    x:weird;
begin
    readln(f,n,q);
    for i:=1 to q do
    begin
        readln(f,b[i].s1,b[i].s2,t,b[i].c);
        for j:=1 to i-1 do
            if b[i].c<b[j].c then
            begin
                x:=b[i];
                b[i]:=b[j];
                b[j]:=x;
            end;
    end;
end;
procedure solve;
var a:array['a'..'f'] of longint;
    s:array[1..100000] of string;
    c:char;
    i,l,r,d:longint;
begin
    fillchar(a,sizeof(a),0);
    s[1]:='a';l:=1;r:=1;d:=0;
    for i:=1 to q do inc(a[b[i].c]);
    for c:='b' to 'f' do a[c]:=a[chr(ord(c)-1)]+a[c];
    while l<=r do
    begin
        if length(s[l])<n then
            if s[l][1]='a' then
                for i:=1 to a['a'] do
                begin
                    inc(r);s[r]:=s[l];
                    delete(s[r],1,1);
                    s[r]:=b[i].s1+b[i].s2+s[r];
                end
            else
                for i:=a[chr(ord(s[l][1])-1)]+1 to a[s[l][1]] do
                begin
                    inc(r);s[r]:=s[l];
                    delete(s[r],1,1);
                    s[r]:=b[i].s1+b[i].s2+s[r];
                end
        else if length(s[l])=n then inc(d);
        inc(l);
    end;
    write(g,d);
end;
begin
    assign(f,fin);reset(f);
    assign(g,fout);rewrite(g);
    readdata;
    solve;
    close(f);close(g);
end.

var
  i,j,k,m,n,t,ans:longint;
  x,y,z,c:char;
  froms,tos:array[1..1000] of longint;
  f:array[1..100,1..100] of longint;
begin
  readln(n,m);
  for i:=1 to m do
  begin
      readln(x,y,c,z);
      froms[i]:=ord(z)-97+1;
      tos[i]:=ord(x)-97+1;
  end;
  f[1,1]:=1;
  for i:=2 to n do
    for j:=1 to m do
      f[i,tos[j]]:=f[i,tos[j]]+f[i-1,froms[j]];
  for i:=1 to 6 do ans:=ans+f[n,i];
  writeln(ans);
end.

var a:array['a'..'f','a'..'f']of char;
    pr,kol,i,j,n,m:longint;
    ss,s:string;
     j1,j2,j3,j4,j5,j6:char;
begin

readln(n,m);
for i:=1 to m do
begin
readln(s);
a[s[1],s[2]]:=s[4];
end;

if n=6 then begin
for j1:='a' to 'f' do
 for j2:='a' to 'f' do
  for j3:='a' to 'f' do
   for j4:='a' to 'f' do
    for j5:='a' to 'f' do
     for j6:='a' to 'f' do
     begin
     s:='';
     s:=j1+j2+j3+j4+j5+j6;
     i:=1;
      while(length(s)>1)do
      begin
        if a[s[i],s[i+1]]in ['a'..'f'] then begin ss:=s;delete(s,i,2);s:=a[ss[i],ss[i+1]]+s;end else break;

      end;
        if s='a' then inc(kol);
     end;
writeln(kol);
end;

if n=5 then begin
for j1:='a' to 'f' do
 for j2:='a' to 'f' do
  for j3:='a' to 'f' do
   for j4:='a' to 'f' do
    for j5:='a' to 'f' do
     begin
     s:='';
     s:=j1+j2+j3+j4+j5;
     i:=1;
      while(length(s)>1)do
      begin
        if a[s[i],s[i+1]]in ['a'..'f'] then begin ss:=s;delete(s,i,2);s:=a[ss[i],ss[i+1]]+s;end else break;

      end;
      if s='a' then inc(kol);
     end;
writeln(kol);
end;

if n=4 then begin
for j1:='a' to 'f' do
 for j2:='a' to 'f' do
  for j3:='a' to 'f' do
   for j4:='a' to 'f' do
     begin
     s:=''; pr:=0;
     s:=j1+j2+j3+j4;
     i:=1;
      while(length(s)>1)do
      begin

        if a[s[i],s[i+1]]in ['a'..'f'] then begin ss:=s;delete(s,i,2);s:=a[ss[i],ss[i+1]]+s;end else break;

      end;
     if s='a'then inc(kol);
     end;
writeln(kol);
end;

if n=3 then begin
for j1:='a' to 'f' do
 for j2:='a' to 'f' do
  for j3:='a' to 'f' do
   begin
     s:='';
     s:=j1+j2+j3;
     i:=1;
      while(length(s)>1)do
      begin
        if a[s[i],s[i+1]]in ['a'..'f'] then begin ss:=s;delete(s,i,2);
        s:=a[ss[i],ss[i+1]]+s;end else break;
        pr:=1;

      end;
      if s='a' then inc(kol);
     end;
writeln(kol);
end;

if n=2 then begin
for j1:='a' to 'f' do
 for j2:='a' to 'f' do
     begin
     s:='';
     s:=j1+j2;
     i:=1;
      while(length(s)>1)do
      begin
        if a[s[i],s[i+1]]in ['a'..'f'] then begin ss:=s;delete(s,i,2);s:=a[ss[i],ss[i+1]]+s;end else break;

      end;
      if s='a' then inc(kol);
     end;
writeln(kol);
end;

end.

var  a,c:array[1..36] of byte;
n,i,q:byte;
k:integer;
x1,x2,x3,x4:char;
procedure dfs(x,y:byte);
var j:byte;
begin
 for j:=1 to q do
  if c[j]=x then
   if y=n then inc(k) else dfs(a[j],y+1);
end;
begin
readln (n,q);
k:=0;
 for i:=1 to q do
  begin
   read(x1,x2,x3,x4);readln;
   a[i]:=ord(x1)-ord('a')+1;
   c[i]:=ord(x4)-ord('a')+1;
  end;
 dfs(1,2);
 writeln(k);
 end.

var  a,c:array[1..36] of byte;
n,i,q:byte;
k:integer;
s:string;
procedure dfs(x,y:byte);
var j:byte;
begin
 for j:=1 to q do
  if c[j]=x then
   if y=n then inc(k) else dfs(a[j],y+1);
end;
begin
readln (n,q);
k:=0;
 for i:=1 to q do
  begin
   readln(s);
   a[i]:=ord(s[1])-ord('a')+1;
   c[i]:=ord(s[4])-ord('a')+1;
  end;
 dfs(1,2);
 writeln(k);
 end.

var  a,b,c:array[1..40] of longint;
s:string;
n,i,k,q:longint;
procedure dfs(x,y:longint);
var j:longint;
begin
 for j:=1 to q do
  if c[j]=x then
   if y=n then inc(k) else dfs(a[j],y+1);
end;
begin
readln (n,q);
k:=0;
 for i:=1 to q do
  begin
   readln(s);
   a[i]:=ord(s[1])-ord('a')+1;
   b[i]:=ord(s[2])-ord('a')+1;
   c[i]:=ord(s[4])-ord('a')+1;
  end;
 dfs(1,2);
 writeln(k);
 end.

program cf653B;
var n,q,k,j,br,i,i2,i3:longint;
    a:array[0..100000] of longint;
    a2,b,c,d,s:array[0..100000] of char;
    zam:boolean;
begin
readln(n,q);
k:=1;
for i:=1 to n do
        k:=k*6;
for i:=1 to q do
        begin
        read(b[i],c[i],d[i],d[i]);
        readln;
        end;

s[0]:='a';
s[1]:='b';
s[2]:='c';
s[3]:='d';
s[4]:='e';
s[5]:='f';

br:=0;

for i:=1 to k do
        begin
        inc(a[n]);
        j:=n;
        while a[j]=6 do
                begin
                a[j]:=0;
                dec(j);
                inc(a[j]);
                end;

        for i2:=1 to n do
                a2[i2]:=s[a[i2]];

        for i2:=1 to n-1 do
                begin
                zam:=false;
                for i3:=1 to q do
                        if (a2[i2]=b[i3]) and (a2[i2+1]=c[i3]) then
                                begin
                                a2[i2+1]:=d[i3];
                                zam:=true;
                                break;
                                end;
                if zam=false then
                        break;
                end;

        if zam and (a2[n]='a') then
                inc(br);
        end;

writeln(br);
end.

var
  n,q,i,j,sum:longint;
  s:string;
  froms,tos:array[1..100] of longint;
  f:array[1..10,1..10] of longint;
begin
  readln(n,q);
  for i:=1 to q do
    begin
      readln(s);
      froms[i]:=ord(s[4])-ord('a')+1;
      tos[i]:=ord(s[1])-ord('a')+1;
    end;
  f[1,1]:=1;
  for i:=2 to n do
    for j:=1 to q do
      f[i,tos[j]]:=f[i,tos[j]]+f[i-1,froms[j]];
  for i:=1 to 6 do sum:=sum+f[n,i];
  writeln(sum);
end.

var   q,i,sz,m,n,ans:longint;
       s:string;
        a,b,c:array[0..1000] of longint;

procedure rek(m,sz:longint);
var i:longint;
 begin

 if sz=n then
  begin
   inc(ans);
   end
  else  begin
 for i:=1 to q do
   if m mod 10=c[i] then
   begin
   inc(sz);
     rek((m-m mod 10+b[i])*10+a[i],sz);

     dec(sz);
     end;
 end;
end;
 begin
 readln(n,q);

 for i:=1 to q do
  begin
  readln(s);
  a[i]:=ord(s[1])-96;
  b[i]:=ord(s[2])-96;
  c[i]:=ord(s[4])-96;
 end;

 m:=1;
 rek(m,1);

writeln(ans);
 end.

const	fi	=	'B.inp';
	fo	=	'B.out';
	maxN	=	1000000;
type	int	=	longint;
	xau	=	string[6];
var	n,q	:	int;
	a,b	:	array[1..36]	of	xau;
	qu 	:	array[1..maxN]	of	xau;
	f 	:	array[1..maxN]	of	boolean;
{}
function mahoa(s: xau) : int;
var n,i,m : int;
begin
	n := length(s);
	m := 0;
	for i:=1 to n do
	   begin
		m := m*10 + ord(s[i]) - 96;
	   end;
	mahoa := m;
	//writeln(s, ' ', m);
end;
{}
procedure xl;
var l,r,i,p,m : int; x,s: xau;
begin
	l:=1; r :=0;
	fillchar(f,sizeof(f),true);
	for i:=1 to q do if b[i]='a' then
	   begin
		inc(r); qu[r] := a[i]; f[mahoa(a[i])] := false;
	   end;
	while l<=r do
	   begin
		x := qu[l]; if length(x)=n then break; inc(l);
		for i:=1 to q do
		   begin
		        p := pos(b[i],x);
			if p=1 then
			    begin
                                s := x;
                                delete(s,p,1);
				insert(a[i],s,p);
				m := mahoa(s);
				if f[m] then begin
				inc(r); qu[r] := s;
				f[m] := false; end;
			    end;
		   end;
	   end;
	writeln(r-l+1);
end;
{}
procedure docdl;
var i: int; s:string;
begin
	readln(n,q);
	for i:=1 to q do
	   begin
		readln(s);
		a[i]:= copy(s,1,2);
		b[i]:= copy(s,4,1);
	   end;
	//for i:=1 to q do writeln(a[i],' ',b[i]);
end;
{}
BEGIN
	//assign(input,fi);	reset(input);
	//assign(output,fo);	rewrite(output);
	docdl;
	xl;
	//close(input);	close(output);
END.

const //fi = 'd:\Dac\CF\653B.inp';
      fi = '';

var  a: array[1..36] of string[2];
     b: array[1..36] of char;
     cach: char;
     n, q, i, res: longint;
     f: text;

procedure tinh(i: longint; kt: char);
var  j: longint;
begin
     if i = n then begin res:= res + 1; exit; end;
     for j:= 1 to q do
        if b[j] = kt then
           begin
              tinh(i + 1, a[j, 1]);
           end;
end;

begin
     assign(f, fi); reset(f);
     readln(f, n, q);
     for i:= 1 to q do
        readln(f, a[i], cach, b[i]);
     close(f);

     res:= 0;
     tinh(1, 'a');

     writeln(res);
     readln;
end.

var
  res:int64;
  a:array[1..36] of string[2];
  b:array[1..36] of string[1];
  n, i, q, len:byte;
  s:string;

Function f(s:string):int64;
var
  i:byte;
  res:int64;
Begin
  if length(s)=n then begin
    res:=1 ;
   // writeln(s);
  end
  else begin
    res:=0;
    for i:=1 to q do
      if s[1]=b[i] then
        res:=res + f(a[i]+copy(s, 2, length(s)-1));
  end;

  f:=res;
End;

BEGIN
  readln(n, q);
  for i:=1 to q do begin
    readln(s);
    a[i]:=copy(s, 1, 2);
    b[i]:=copy(s, 4, 1);
  end;

  res:=0;
  res:=f('a');

  write(res);

END.

  var

       cnt,i,n,q:longint;

       s,a,b:array[1..100] of ansistring;

         used:array[1..1000] of char;

           subs:ansistring;

             g:boolean;

       procedure  check;

         begin

           subs:='';

           for i:=1 to n do subs:=subs+used[i];

             while length(subs)>1 do begin

               g:=false;

                 for i:=1 to q do

                   if (subs[1]=a[i][1]) and (subs[2]=a[i][2]) then begin delete(subs,1,2); subs:=b[i]+subs; g:=true; break; end;

                if g=false then exit;

             end;

               if (length(subs)=1) and (subs='a') then begin inc(cnt); exit; end;

           end;

      procedure pereb(step:longint);

        begin

          if step>n then begin check; exit; end;

            used[step]:='a';

              pereb(step+1);

            used[step]:='b';

              pereb(step+1);

          used[step]:='c';

              pereb(step+1);

          used[step]:='d';

              pereb(step+1);

          used[step]:='e';

              pereb(step+1);

          used[step]:='f';

              pereb(step+1);

        end;

    begin
      readln(n,q);
        for i:=1 to q do readln(s[i]);

          for i:=1 to q do begin

            a[i]:=copy(s[i],1,2);

              b[i]:=copy(s[i],4,1);

          end;

            pereb(1);

          write(cnt);

            end.

var
a:array['a'..'f','a'..'f'] of longint;
f:array[1..6,'a'..'f'] of int64;
all:array[1..6] of int64;
i,j,p,t:char;
k,n,len:longint;
i1,i2:longint;
ans:int64;
 s:string;
begin

for i:='a' to 'f' do
for j:='a' to 'f' do
a[i,j]:=0;

for i1:=1 to 6 do
for i:='a' to 'f' do
f[i1,i]:=0;

readln(len,n);
for k:=1 to n do
        begin
        readln(s);
        p:=s[1];
        t:=s[4];
        inc(a[t,p]);
        end;
        {
        for i:='a' to 'f' do
        begin
        for j:='a' to 'f' do
        write(a[i,j],' ');
        writeln;
        end;
         }
all[2]:=a['a','a']+a['a','b']+a['a','c']+a['a','d']+a['a','e']+a['a','f'];
//writeln(all[2]);
for i:='a' to 'f' do
begin
f[2,i]:=a['a',i];
//write(f[2,i],'  ');
end;
   for i1:=2 to len-1 do
   begin
        for i:='a' to 'f' do
        begin
                for j:='a' to 'f' do
                f[i1+1,j]:=f[i1+1,j]+f[i1,i]*a[i,j];
        end;
   end;
   {
   writeln;
   for i1:=2 to len do
   begin
   for j:='a' to 'f' do
   write(f[i1,j],' ');
   writeln;
   end;
   }
ans:=0;
for i:='a' to 'f' do
ans:=ans+f[len,i];
writeln(ans);
end.

var
    n,m,he,ta,i,j,k,ans:longint;
    s:ansistring;
    q,a,b:array[0..2000000] of ansistring;
    o:array[0..2000000] of boolean;

function hash(s:ansistring):longint;
var
    t,p,i:longint;
begin
    p:=1;
    t:=0;
    for i:=length(s) downto 1 do
    begin
        t:=t+p*(ord(s[i])-ord('a')+1);
        p:=p*7
    end;
    exit(t)
end;

procedure bfs;
var
    i,j:longint;
    u:ansistring;
begin
    q[1]:='a';
    o[1]:=true;
    he:=0;
    ta:=1;
    while he<ta do
    begin
        he:=he+1;
        if (length(q[he])=n) then
            break;
        for i:=1 to m do
        begin
            j:=1;
                if q[he][j]=b[i] then
                begin
                    u:=a[i]+copy(q[he],2,length(q[he])-1);
                    if (not o[hash(u)]) then
                    begin
                        o[hash(u)]:=true;
                        ta:=ta+1;
                        q[ta]:=u;
                    end
                end
        end
    end
end;

begin
    readln(n,m);
    for i:=1 to m do
    begin
        readln(s);
        a[i]:=copy(s,1,2);
        b[i]:=s[4]
    end;
    bfs;
    for i:=1 to ta do
        if length(q[i])=n then
            ans:=ans+1;
    writeln(ans)
end.

Program Main;
  var n , q : LongInt;
       A : Array [ 1 .. 36 ] of String[2];
       B : Array [ 1 .. 36 ] of Char;
       ans : LongInt;
       br : String [ 6 ] ;

  procedure Check ( ) ;
    var z : String;
         f : boolean;
         i : LongInt;

  begin
    z := br;

    while ( length(z) > 1 ) do
    begin
      f := false;
      for i := 1 to q do
      begin
        if ( z[1] + z[2] = A[i] ) then
        begin
          Delete ( z , 1 , 1 ) ;
           z[1] := B[i];
           f := true;
           break;
        end;

      end;

      if ( f = false ) then
        exit();

    end;

    if ( z = 'a' ) then
      inc ( ans ) ;

  end;

  procedure Brute ( v : LongInt ; c : char ) ;
  begin
    br[v] := c;
    if ( v = n ) then
      Check ()
    else
      Brute ( v + 1 , 'a' ) ;

    if ( c <> 'f' ) then
      Brute ( v , Chr(Ord(c) + 1 ) ) ;
  end;

       var i : LonGInt;
       s : String;
begin
  readLn ( n , q ) ;

  for i := 1 to q do
  begin

    readLn ( s ) ;
    A[i] := S[1] + S[2];
    B[i] := S[4];

  end;

  br := '';

  for i := 1 to N do
    br := br + ' ' ;

  Brute ( 1 , 'a' ) ;

  writeLn ( ans ) ;

end.

type long=array[0..6,0..6,0..6,0..6,0..6,0..6] of longint;
procedure make(var s:String;n:longint);
var
i:longint;
begin
s[n]:=chr(ord(S[n])+1);
i:=n;
while(i>=2)and(s[i]='g')do begin
s[i]:='a';
s[i-1]:=chr(ord(s[i-1])+1);
dec(i);
end;
end;
procedure may(s:string;var a,b,c,d,e,f:longint);
begin
a:=0;
b:=0;
c:=0;
d:=0;
e:=0;
f:=ord(s[length(S)])-96;
if length(S)>=2
then e:=ord(s[length(S)-1])-96;
if length(S)>=3
then d:=ord(s[length(S)-2])-96;
if length(S)>=4
then c:=ord(s[length(S)-3])-96;
if length(S)>=5
then b:=ord(s[length(S)-4])-96;
if length(S)>=6
then a:=ord(s[length(S)-5])-96;
end;
var
a,b:array[1..36] of string;
n,q,i,sum,j,k1,k2,k3,k4,k5,k6,k11,k22,k33,k44,k55,k66:longint;
s,s1,s2:string;
can:long;
begin
readln(n,q);
for i:=1 to n-1 do
s:=s+'a';
s:=s+(chr(96));
for i:=1 to q do begin
readln(s1);
a[i]:=s1[1]+s1[2];
b[i]:=s1[4];
end;
for j:=n downto 2 do begin
s:='';
for i:=1 to j-1 do
s:=s+'a';
s:=s+(chr(96));
while true do begin
make(s,j);
if s[1]='g'
then break;
may(s,k1,k2,k3,k4,k5,k6);
if length(S)=n
then can[k1,k2,k3,k4,k5,k6]:=1;
for i:=1 to q do begin
s1:=s;
if  j=2
then inc(sum);
if copy(s1,1,2)=a[i]
then begin
delete(s1,1,2);
s1:=b[i]+s1;
may(s1,k11,k22,k33,k44,k55,k66);
inc(can[k11,k22,k33,k44,k55,k66],can[k1,k2,k3,k4,k5,k6]) ;
end;
end;
end;
end;
writeln(can[0,0,0,0,0,1]);
end.

var s:string; j,i,p,n,q:integer; a:array[1..2,1..36] of string;
function col(m:string; z:integer):integer;
var t,o:integer;
 begin
 t:=0;
 if z=n then col:=1
 else
  begin
  for o:=1 to q do
   begin
   if a[2,o]=m then t:=col(a[1,o][1],z+1)+t;
   end;
   col:=t;
  end;
 end;
begin
readln(n,q);
for i:=1 to q do
begin
readln(s);
a[1,i]:=s[1]+s[2];
a[2,i]:=s[4];
end;

write(col('a',1));
end.

const
   tfi='';
   tfo='';

type TE= record
   v, link: longint; end;

var
   fi, fo: text;
   n, m: longint;
   res: int64;
   head: array[0..6] of longint;
   e: array[0..100] of TE;
   free: array[0..666666] of boolean;

function get(s: string; i, j: longint): longint;
var
   tmp: string;
   k,x: longint;
begin
   tmp :='';
   for k:=i to j do
      begin
         x:= ord(s[k])-48;
         tmp:=tmp+char(x);
      end;

   val(tmp, x);
   exit(trunc(x));
end;

procedure __;
var
   i: longint;
   s: string;
begin
   assign(fi, tfi); reset(fi);
   assign(fo, tfo); rewrite(fo);
   readln(fi, n, m);
   for i:=1 to m do
      begin
         readln(fi, s);
         e[i].v := get(s,1,2);
         e[i].link := head[get(s,4,4)];
         head[get(s,4,4)] := i;
      end;
end;

function check(value: longint): boolean;
var
   tmp: string;
begin
   str(value, tmp);
   if length(tmp)=n then exit(true);
   exit(false);
end;

procedure dfs(u, value: longint);
var
   i, x: longint;
   tmp,s1 : string;
begin
   if check(value) then
      begin
         inc(res); exit;
      end;
   i := head[u]; tmp := '';
   while i<>0 do
      begin
         str(value, tmp);
         str(e[i].v, s1);
         tmp := copy(tmp,2, length(tmp));
         tmp:=s1+tmp;
         val(tmp, x);
         if not free[x] then
            dfs(ord(tmp[1])-48,x );
         i := e[i].link;
      end;
end;

procedure xuly;
var
   i: longint;
begin
   i := head[1];
   while i<>0 do
      begin
         dfs(e[i].v div 10, e[i].v);
         i := e[i].link;
      end;
   writeln(fo, res);
end;

procedure _;
begin
   close(fi); close(fo);
end;

BEGIN
   __;
   xuly;
   _;
END.

type
  stroka = record
    a, b, c: char;
  end;

var
  a, b, c, i, o: int64;
  t: char;
  ar: array [1..1000] of stroka;

procedure gen(s1: string);
var
  i: int64;
  s2: string;

begin
  if length(s1) = a then begin
    inc(o);
    exit;
  end;
  if length(s1) > a then exit;
  i := 1;
  while i <= b do begin
    if s1[1] = ar[i].c then begin
      s2 := s1;
      s2[1] := ar[i].b;
      s2 := ar[i].a + s1;
      gen(s2);
    end;
    inc(i);
  end;
end;

begin
  readln(a, b);
  i := 1;
  o := 0;
  while i <= b do begin
    readln(ar[i].a, ar[i].b, t, ar[i].c);
    inc(i);
  end;
  gen('a');
  writeln(o);
end.

var a,b,k:array [1..100] of longint;
c:array [1..100,1..100] of longint;
n,z,q,i,j:longint;
s:string;
begin
readln(n,q);
for i:=1 to q do
 begin
  readln (s);
  a[i]:=(ord(s[1])-96)*10+ord(s[2])-96;
  b[i]:=ord(s[4])-96;
 end;
for i:=1 to q do
 if b[i]=1 then begin inc(k[2]); c[2,k[2]]:=a[i] div 10; end;
for i:=3 to n do
 for j:=1 to k[i-1] do
  for z:=1 to q do
   if b[z]=c[i-1,j] then begin inc(k[i]); c[i,k[i]]:=a[z] div 10; end;
write(k[n]);
end.

var a,b:array [1..100] of longint;
q,n,i,i1,k:longint;
s:string;
procedure dfs(x,y:longint);
 begin
  for i1:=1 to q do
   begin
    if (y=n) and (b[i1]=x) then inc(k) else
    if (b[i1]=x) and (y<n) then dfs(a[i1],y+1);
   end;
 end;
begin
readln(n,q);
for i:=1 to q do
 begin
  readln (s);
  a[i]:=ord(s[1])-96;
  b[i]:=ord(s[4])-96;
 end;
if n=2 then
         begin
          for i:=1 to q do
           if b[i]=1 then inc(k);
          write(k);
          exit;
         end;
for i:=1 to q do
 if b[i]=1 then dfs(a[i],3);
write (k);
end.

var a,b:array [1..100] of longint;
q,n,i,i1,k:longint;
s:string;
procedure dfs(x,y:longint);
 begin
  for i1:=1 to q do
   begin
    if (y=n) and (b[i1]=x) then inc(k) else
    if (b[i1]=x) and (y<n) then dfs(a[i1],y+1);
   end;
 end;
begin
readln(n,q);
for i:=1 to q do
 begin
  readln (s);
  a[i]:=ord(s[1])-96;
  b[i]:=ord(s[4])-96;
 end;
for i:=1 to q do
 if b[i]=1 then dfs(a[i],3);
write (k);
end.

Const Finp='';
      Fout='';
      maxCase=46656;
Var n:longint;
    change:array['a'..'f','a'..'f'] of string;
    hd:array[1..maxcase] of string;

Procedure Nhap;
Var f:Text;
    i,q:longint;
    a,b,s:string;
Begin
    assign(f,finp); reset(f);
    readln(f,n,q);
    for i:=1 to q do
        begin
            readln(f,s);
            a:=s[1]+s[2]; b:=s[4];
            if (a[1]<='f') or (a[2]<='f') or (b<='f') then
                change[a[1],a[2]]:=b;
        end;
    close(f);
End;
Function DecToSen(x:longint):string;
Var s:string;
Begin
    s:='';
    while x>0 do
        begin
            s:=chr((x mod 6)+97)+s;
            x:=x div 6;
        end;
    dectosen:=s;
End;
Function Bfs(s:string):boolean;
Var l,r,i,j:longint;
    s1:string;
    c1,c2:char;
    lc:boolean;
Begin
    l:=1; r:=1; hd[1]:=s;
    while l<=r do
        begin
            s1:=hd[l];
            for i:=1 to length(s1)-1 do
                if change[s1[i],s1[i+1]]<>'' then
                    begin
                        c1:=s1[i]; c2:=s1[i+1];
                        delete(s1,i,2);
                        insert(change[c1,c2],s1,i);
                        lc:=true;
                        for j:=1 to i-1 do
                            if hd[j]=s1[i] then
                                begin
                                    lc:=false;
                                    break;
                                end;
                        if lc=true then
                            begin
                                inc(r);
                                hd[r]:=s1;
                            end;
                        if hd[r]='a' then
                            exit(true);
                        break;
                    end;
            inc(l);
        end;
    bfs:=false;
End;
Function Dem:longint;
Var i,d:longint;
    s:string;
Begin
    d:=0;
    for i:=0 to maxcase do
        begin
            s:=dectosen(i);
            if length(s)>n then
                break;
            while length(s)<n do
                s:='a'+s;
            if bfs(s)=true then
                begin
                    inc(d);
                    continue;
                end;
        end;
    dem:=d;
End;
Procedure Xuat;
Var f:Text;
Begin
    assign(f,fout); rewrite(f);
    write(f,dem);
    close(f);
End;

Begin
    nhap;
    xuat;
End.

var
a:array[1..1000,1..1000] of string;
p,q:array[1..40] of string;
t:array[1..10] of longint;
s:string;
n,l,i,j,j1:longint;
begin
read(n); readln(l);
t[1]:=1;
a[1][1]:='a';
for i:=1 to l do
begin
readln(s);
p[i]:=copy(s,1,2);
q[i]:=copy(s,4,1);
end;
for i:=1 to n-1 do
begin
        for j:=1 to t[i] do
                for j1:=1 to l do
                begin
                s:=a[i][j];
                if(s[1]=q[j1]) then
                begin
                inc(t[i+1]);
                a[i+1][t[i+1]]:=p[j1];
                a[i+1][t[i+1]]:=a[i+1][t[i+1]]+copy(s,2,length(s)-1);
                end;
                end;
end;
writeln(t[n]);
end.

var
    n,m,he,ta,i,j,k,ans:longint;
    s:ansistring;
    q,a,b:array[0..2000000] of ansistring;
    o:array[0..2000000] of boolean;

function hash(s:ansistring):longint;
var
    t,p,i:longint;
begin
    p:=1;
    t:=0;
    for i:=length(s) downto 1 do
    begin
        t:=t+p*(ord(s[i])-ord('a'));
        p:=p*6
    end;
    exit(t)
end;

procedure bfs;
var
    i,j:longint;
    u:ansistring;
begin
    q[1]:='a';
    o[0]:=true;
    he:=0;
    ta:=1;
    while he<ta do
    begin
        he:=he+1;
        if (length(q[he])=n) then
            break;
        for i:=1 to m do
        begin
            j:=1;
                if q[he][j]=b[i] then
                begin
                    u:=a[i]+copy(q[he],2,length(q[he])-1);
                    if (not o[hash(u)]) then
                    begin
                        o[hash(u)]:=true;
                        ta:=ta+1;
                        q[ta]:=u;
                    end
                end
        end
    end
end;

begin
    readln(n,m);
    for i:=1 to m do
    begin
        readln(s);
        a[i]:=copy(s,1,2);
        b[i]:=s[4]
    end;
    bfs;
    for i:=1 to ta do
        if length(q[i])=n then
            ans:=ans+1;
    writeln(ans)
end.

var
    n,m,he,ta,i,j,k,ans:longint;
    s:ansistring;
    q,a,b:array[0..200000] of ansistring;
    o:array[0..2000000] of boolean;

function hash(s:ansistring):longint;
var
    t,p,i:longint;
begin
    p:=1;
    t:=0;
    for i:=length(s) downto 1 do
    begin
        t:=t+p*(ord(s[i])-ord('a'));
        p:=p*6
    end;
    exit(t)
end;

procedure bfs;
var
    i,j:longint;
    u:ansistring;
begin
    q[1]:='a';
    o[0]:=true;
    he:=0;
    ta:=1;
    while he<ta do
    begin
        he:=he+1;
        if (length(q[he])=n) then
            break;
        for i:=1 to m do
        begin
            j:=1;
                if q[he][j]=b[i] then
                begin
                    u:=copy(q[he],1,j-1)+a[i]+copy(q[he],j+1,length(q[he])-j);
                    if (not o[hash(u)]) then
                    begin
                        o[hash(u)]:=true;
                        ta:=ta+1;
                        q[ta]:=u;
                    end
                end
        end
    end
end;

begin
    readln(n,m);
    for i:=1 to m do
    begin
        readln(s);
        a[i]:=copy(s,1,2);
        b[i]:=s[4]
    end;
    bfs;
    for i:=1 to ta do
        if length(q[i])=n then
            ans:=ans+1;
    writeln(ans)
end.