### Problem Statement

Consider a string T containing exactly L characters. The string T(i) is a cyclic shift of T starting from position i (0 <= i < L). T(i) contains exactly the same number of characters as T. For each j between 0 and L-1, inclusive, character j of T(i) is equal to character (i+j)%L of T. Let's call T a magic word if there are exactly K positions i such that T(i) = T.

You are given a String[] S containing exactly N words. For each permutation p = (p, p, ..., p[N-1]) of integers between 0 and N-1, inclusive, we can define a string generated by this permutation as a concatenation S[p] + S[p] + ... + S[p[N-1]]. Return the number of permutations that generate magic words. All indices in this problem as 0-based.

### Definition

 Class: MagicWords Method: count Parameters: String[], int Returns: int Method signature: int count(String[] S, int K) (be sure your method is public)

### Constraints

-S will contain between 1 and 8 elements, inclusive.
-Each element of S will contain between 1 and 20 characters, inclusive.
-Each element of S will contain only uppercase letters ('A'-'Z').
-K will be between 1 and 200, inclusive.

### Examples

0)

 `{"CAD","ABRA","ABRA"}` `1`
`Returns: 6`
 Every permutation generates a magic word here.
1)

 `{"AB","RAAB","RA"}` `2`
`Returns: 3`
 The magic words are "ABRAABRA" and "RAABRAAB". The first word is generated only by the permutation (0, 1, 2), and the second word is generated by the two permutations (1, 2, 0) and (2, 0, 1).
2)

 `{"AA","AA","AAA","A"}` `1`
`Returns: 0`
 All permutations generate the string "AAAAAAAA" and it clearly is not a magic word because all its cyclic shifts are the same as the original string.
3)

 `{"AA","AA","AAA","A","AAA","AAAA"}` `15`
`Returns: 720`
4)

 `{"ABC","AB","ABC","CA"}` `3`
`Returns: 0`
5)

 `{"A","B","C","A","B","C"}` `1`
`Returns: 672`
6)

 ```{"AAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAB"}``` `1`
`Returns: 40320`

#### Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10195

#### Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13695&pm=10195

gojira_tc

#### Testers:

PabloGilberto , Olexiy , ivan_metelsky

#### Problem categories:

Brute Force, String Manipulation