import java.io.*;
import java.util.*;

public class JD {

	public static void main(String [] args)
	{
		String post, pre;
		int  m;

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			String line ="";
			try {
				line = in.readLine();
			} catch (Exception e) {};
			StringTokenizer str = new StringTokenizer(line);
			m = Integer.parseInt(str.nextToken());
			if (m == 0)
				break;
			pre = str.nextToken();
			post = str.nextToken();
			if (pre.length() != post.length())
				System.out.println(0);
			else
				System.out.println(process(m, pre, post));
		}
	}

	public static long process(int m, String pre, String post)
	{
		if (pre.charAt(0) != post.charAt(post.length()-1))
			return 0;
		if (pre.length() == 1)
			return 1;
		long ans = 1;
		pre = pre.substring(1, pre.length());
		post = post.substring(0, post.length()-1);
		int numchild = 0;
		long comb = 1;
		while (pre.length() > 0) {
			numchild++;
			int index = post.indexOf(pre.charAt(0));
			if (index == -1) {
	//			cout << "Ooops, this should never happen: " << pre << ',' << post << endl;
	//			exit(-1);
				return 0;
			}
			String newPost = post.substring(0, index+1);
			post = post.substring(index+1, post.length());
			String newPre = pre.substring(0, index+1);
			pre = pre.substring(index+1, pre.length());
			ans *= process(m, newPre, newPost);
			if (ans == 0)
				return 0;
			comb = comb*(m-numchild+1)/numchild;
		}
		return ans*comb;
	}


}
