/* Solution to pre-post by bob roos
*/

#include <stdio.h>
#include <string.h>

int m;
char pre[27], post[27];

int comb(int n, int k)
{
  double prod = 1.0;
  int i;

  for (i = 1; i <= k; i++)
    prod *= (n-i+1)/(double)i;
  return (int)(prod+.5);
}

int find(char *w, char c)
{
  int i;

  i = 0;
  while (i < strlen(w) && w[i] != c)
    i++;
  return i;
}

int answer()
{
  int i, j, prod, count[26], loc;
  char c,d, trans[27];
  
/*printf("m = %d, pre = %s, post = %s\n",m,pre,post);
*/
  for (i = 0; i < strlen(pre); i++)
  {
    c = pre[i] - 'a'; /* actual alphabet position of pre[i] */
    trans[c] = i+'a'; /* rename nodes in pre s.t. pre = "abcd..." */
    count[i] = 0; /* child count of (renamed) node */
  }


  for (i = 0; i < strlen(pre); i++)
  {
    c = pre[i]; /* next node in tree */
    loc = find(post,c); /* find its location in post */
/* printf("loc(%s,%c) = %d\n",post,c,loc);
*/
    c = trans[c - 'a'] - 'a';
    for (j = loc+1; j < strlen(post); j++)
    {
      d = trans[post[j] - 'a']-'a';
      if (d <= i)
      {
        count[d]++;
        break;
      }
    }
  }

  prod = 1;
  for (i = 0; i < strlen(post); i++)
    prod *= comb(m,count[i]);
  return prod;
}

int main()
{
  while (scanf("%d",&m) && m > 0)
  {
    scanf("%s",pre);
    scanf("%s",post);
    printf("%d\n",answer());
  }
  return 0;
}

