#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;

class MagicWords {
public:
	int count(vector <string> S, int K) 
	{
		int cnt = 0;

		int idx[] = {0,1,2,3,4,5,6,7};
		do {
			string s; for(int i=0; i<S.size(); ++i) s += S[idx[i]];

			if( s.size()%K==0 && rep_of(s, s.size()/K) )
			{
				for(int n=1; n<s.size()/K; ++n)
					if( s.size()%n==0 && rep_of(s, n) )
						goto fail;
				++cnt;
			fail:;
			}
		} while( next_permutation(idx+0, idx+S.size()) );

		return cnt;
	}

	bool rep_of( string& s, int n ) {
		return s == s.substr(n, s.size()-n) + s.substr(0, n);
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string S_[] = {"CAD","ABRA","ABRA"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int K = 1; 
	int RetVal = 6; 
	return verify_case(RetVal, MagicWords().count(S, K)); }
int Test_(Case_<1>) {
	string S_[] = {"AB","RAAB","RA"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int K = 2; 
	int RetVal = 3; 
	return verify_case(RetVal, MagicWords().count(S, K)); }
int Test_(Case_<2>) {
	string S_[] = {"AA","AA","AAA","A"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int K = 1; 
	int RetVal = 0; 
	return verify_case(RetVal, MagicWords().count(S, K)); }
int Test_(Case_<3>) {
	string S_[] = {"AA","AA","AAA","A","AAA","AAAA"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int K = 15; 
	int RetVal = 720; 
	return verify_case(RetVal, MagicWords().count(S, K)); }
int Test_(Case_<4>) {
	string S_[] = {"ABC","AB","ABC","CA"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int K = 3; 
	int RetVal = 0; 
	return verify_case(RetVal, MagicWords().count(S, K)); }
int Test_(Case_<5>) {
	string S_[] = {"A","B","C","A","B","C"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int K = 1; 
	int RetVal = 672; 
	return verify_case(RetVal, MagicWords().count(S, K)); }
int Test_(Case_<6>) {
	string S_[] = {"AAAAAAAAAAAAAAAAAAAA",
 "AAAAAAAAAAAAAAAAAAAA",
 "AAAAAAAAAAAAAAAAAAAA",
 "AAAAAAAAAAAAAAAAAAAA",
 "AAAAAAAAAAAAAAAAAAAA",
 "AAAAAAAAAAAAAAAAAAAA",
 "AAAAAAAAAAAAAAAAAAAA",
 "AAAAAAAAAAAAAAAAAAAB"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int K = 1; 
	int RetVal = 40320; 
	return verify_case(RetVal, MagicWords().count(S, K)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE

