One day little Vasya found mom's pocket book. The book had n names of her friends and unusually enough, each name was exactly m letters long. Let's number the names from 1 to n in the order in which they are written.
As mom wasn't home, Vasya decided to play with names: he chose three integers i, j, k (1 ≤ i < j ≤ n, 1 ≤ k ≤ m), then he took names number i and j and swapped their prefixes of length k. For example, if we take names "CBDAD" and "AABRD" and swap their prefixes with the length of 3, the result will be names "AABAD" and "CBDRD".
You wonder how many different names Vasya can write instead of name number 1, if Vasya is allowed to perform any number of the described actions. As Vasya performs each action, he chooses numbers i, j, k independently from the previous moves and his choice is based entirely on his will. The sought number can be very large, so you should only find it modulo 1000000007 (109 + 7).
Input
The first input line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of names and the length of each name, correspondingly. Then n lines contain names, each name consists of exactly m uppercase Latin letters.
Output
Print the single number — the number of different names that could end up in position number 1 in the pocket book after the applying the procedures described above. Print the number modulo 1000000007 (109 + 7).
Examples
Input
2 3
AAB
BAA
Output
4
Input
4 5
ABABA
BCGDG
AAAAA
YABSA
Output
216
Note
In the first sample Vasya can get the following names in the position number 1: "AAB", "AAA", "BAA" and "BAB".
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to understand the operation: we have n strings, each of length m. Operation: choose i, j (i < j) and k (1 ≤ k ≤ m). Then swap the prefixes of length k between string i and string j. So after the operation, the first k characters of string i become the first k of string j, and vice versa.
We can perform any number of operations. We are interested in the set of possible strings that can appear at position 1 (the first string) after any sequence of operations.
We need to count the number of distinct strings that can be in position 1.
Constraints: n, m ≤ 100. Strings consist of uppercase letters (26 letters). The answer can be large, modulo 1e9+7.
We need to analyze the group of operations. The operations essentially allow us to permute characters among strings? But with constraints: swapping prefixes of length k between two strings. This operation is akin to a certain transformation on the set of strings.
We have n strings, each of length m. Think of each string as a vector of characters. Operation: for some i,j,k, exchange the first k entries of vector i with the first k entries of vector j. This is like we have n rows of length m, and we can perform swaps of initial segments between two rows.
We want to know what strings can appear in row 1 after any sequence.
Observations:
The operation is reversible (swap again with same i,j,k to revert). So the set of reachable configurations forms a group acting on the set of all n-tuples of strings.
But we only care about the set of possible strings at row 1, irrespective of others.
We need to characterize the equivalence classes or orbits under these operations.
This seems like a combinatorial group generated by prefix swaps.
Alternate viewpoint: We have n strings, each of length m. Consider the multiset of columns? Because swapping prefixes of length k between two strings essentially swaps the first k columns for those two rows? Not exactly columns; it's swapping initial segments row-wise. But it resembles operations on a matrix where we can exchange certain segments.
Another viewpoint: For each column position t (1..m), the characters at that column across rows might be permuted? But the operation swaps a prefix of length k, meaning for all positions 1..k, the characters of strings i and j are swapped simultaneously. So it's like we can perform simultaneous swaps of the first k columns between two rows. So it couples positions.
But note that swapping the entire strings is possible by taking k=m. So we can swap any two whole strings. Therefore, the set of strings that can appear at position 1 is exactly the set of strings that appear somewhere in the multiset? Not exactly, because we can also combine parts of strings via prefix swaps. For instance, from the example: two strings "AAB" and "BAA". They got "AAA" and "BAB" as well. So more strings can be formed.
So we can mix prefixes from different strings.
Essentially, the operation allows us to recombine prefixes from different strings arbitrarily? Let's think.
We have a collection of n strings. Operation: pick two strings, and swap their prefixes of length k. This is similar to performing a cut-and-paste: you can take the first k characters from one string and exchange them with the first k from another. This can be repeated.
If we think of each string as a sequence of characters, we can perform swaps of leading substrings. This is reminiscent of the "stack" or "queue"? Actually, it's like we have n piles (strings) of length m, each is a list. We can exchange the top k elements of two piles. This is similar to the operation in the "parallel stacks" problem.
We want to know what strings can be formed in pile 1.
Let’s denote the characters as items. But note that characters are not unique; they can be repeated.
We can think of each position (index) as having a multiset of characters from the strings. Initially, each column has a list of characters (the letters at that position across strings). However, after prefix swaps, the characters can move across positions? Because swapping prefixes swaps the first k positions between two rows, thus moving characters across columns (but only within the prefix region). For example, if we swap prefixes of length k between string i and j, the character originally at position p (1 ≤ p ≤ k) of string i moves to position p of string j. So it stays in same column p, but moves to a different row. So column-wise: characters in column p are permuted among rows. In fact, swapping prefixes of length k simultaneously swaps the contents of columns 1..k between the two rows. So each operation is a permutation on the set of n characters in each column 1..k, but the permutation is the same for all those columns (i.e., swapping the two rows). So it is a transposition of rows i and j, but only for columns 1..k.
Thus, the operation acts on the matrix (rows i, columns 1..m) by swapping the submatrix rows i and j on columns 1..k.
So we can perform any permutation of rows on a prefix of columns? Since we can compose such swaps? For fixed k, swapping prefixes of length k is like having the ability to swap the first k columns between two rows. By composing such swaps, we can realize any permutation of the rows restricted to those columns? For example, if we want to permute the rows arbitrarily on the first k columns, can we do that with a series of swaps? Yes, because swapping rows i and j on those columns is a transposition; transpositions generate the full symmetric group. So for any given k, we can permute the rows arbitrarily on the first k columns, using a sequence of swaps (each swap is between two rows, swapping their first k columns). But careful: each swap operation also swaps the first k columns of the two rows simultaneously; by composing such swaps, we can achieve any permutation of the rows for those columns. So indeed, the group generated by operations with a particular k allows us to arbitrarily permute the rows restricted to columns 1..k, independent of other columns. However, note that operations can also be performed with different k's, mixing up the interactions.
Thus, we have the ability to permute rows for any prefix length, but each operation applies only to a specific prefix length at a time. But using compositions, we could potentially achieve more complex rearrangements.
Let’s think about the total effect: We have a matrix A of size n x m (rows are strings, columns are positions). We can perform operations: for any i<j and any k, swap A[i,1..k] with A[j,1..k].
This is similar to the action of the group of "prefix swaps" which might be equivalent to being able to independently permute rows within each column, but with a constraint: the permutations across columns must be consistent in some way? Actually, if we consider each column separately, we could wish to permute rows arbitrarily in that column. But does the operation allow that? For column p (p from 1 to m), if we only want to swap A[i,p] and A[j,p], can we do that? We could perform a swap with k = p (or k ≥ p?). If we swap prefixes of length k, then for all columns 1..k, the rows i and j are swapped. So swapping just a single column p would also swap all columns before p. That might not be allowed if we want to keep those other columns unchanged. However, we can later correct those other columns by additional operations.
So maybe we can achieve any permutation of the multiset of characters per column, subject to certain constraints linking columns? The example shows that we can create new strings that were not originally present, like "AAA" and "BAB" from "AAB" and "BAA". Let's examine:
Original:
1: A A B
2: B A A
Goal: "AAA" at pos1. How can we get? Swap prefix of length 1? Swap first character: after swap: 1: B A B, 2: A A A. Not yet. Or swap prefix length 2: swapping first two characters: 1: B A B? Actually compute: originally 1: A A B; 2: B A A. swap first two: 1 becomes B A B? Wait, first two of 1 become first two of 2: so 1 becomes B A (from 2) plus third char of 1 (B) => B A B. 2 becomes A A (from 1) plus third char of 2 (A) => A A A. So after swap length 2, position2 gets "AAA". We want "AAA" at position1, we can then swap whole strings (k=3) to bring it to position1. So "AAA" appears.
"BAB": After swapping first two? That gave "BAB" at position1? Actually after swapping first two, position1 becomes "BAB". So directly. So indeed, we can recombine.
Thus, the set of reachable strings in row 1 is all strings of length m that can be formed by taking for each position p a character from some original row, but with a consistency condition: For each prefix, the multiset of characters taken from that prefix must correspond to something? Let's try to formalize.
Consider that the operations allow us to permute rows independently per column, but the permutations must be "consistent" in the sense that they come from a sequence of prefix swaps. However, maybe the reachable configurations are exactly those where the matrix's rows after operations are such that the rows are some "shuffling" of the original rows according to a "suffix-consistent" permutation? I recall a known combinatorial structure: swapping prefixes between strings leads to the property that the set of rows, considered as strings, after any such operations, is such that the multiset of suffixes (from any position) remains invariant? Something like that.
Think about the following: When we swap prefixes of length k between rows i and j, the multiset of suffixes of length m-k (positions k+1..m) of those two rows remains unchanged? Actually, after swapping, the suffix (positions k+1..m) of row i stays the same (since we swapped only prefix), but the prefix changed. So the suffix part stays attached to the same row? Wait: For row i, after swapping prefixes of length k with row j, row i gets prefix from row j and its original suffix (positions k+1..m) unchanged. Similarly row j gets prefix from i and its original suffix unchanged. So the suffix of row i stays with row i (the row index). So suffixes are not exchanged. Thus, the suffix of each row (starting at position k+1) remains attached to the same row. However, later operations may affect longer prefixes that include that suffix region? If we later do a swap with a larger k' > k, it will swap prefixes that include some of that suffix region. So suffixes can eventually move.
But maybe there is an invariant: Consider the multiset of all strings (rows). Does it change? When we swap prefixes, the multiset of whole strings may change because we are creating new strings. Indeed, the multiset of whole strings changes; new strings appear. So that's not invariant.
Maybe there is an invariant based on the "column multiset" after sorting rows? Let's examine column-wise: After any operation, the multiset of characters in each column stays the same? Because swapping prefixes between rows i and j swaps the characters in columns 1..k between those two rows. For a fixed column p, the set of characters in that column across rows is unchanged because we just swapped two entries. So the multiset of characters in each column is invariant. Indeed, each operation swaps two entries within each of the first k columns. So for each column p, the multiset of symbols across rows is invariant. So the column multisets are fixed.
Thus, if we denote C_p = multiset of letters in column p (over all n strings), then C_p is invariant throughout operations.
Therefore, any reachable configuration must have exactly those column multisets.
Now, what about the row strings? They can be any combination of picking one character from each column, such that for each column p, the characters assigned to rows form exactly the multiset C_p, and also there may be additional constraints due to the operation structure beyond column independence.
The question: Does the operation allow us to achieve any assignment of characters to rows consistent with column multisets? That is, can we realize any matrix with the same column multisets as the original? If yes, then the number of distinct strings possible at row 1 would be simply the number of distinct strings that can appear as a row in any matrix with those column multisets. And since we can permute rows arbitrarily (by swapping whole strings), the set of strings that can appear at row 1 is precisely the set of all strings that can be formed by taking one character from each column, using the available multiset per column, but with the condition that there exists a full assignment (n rows) matching all column multisets. However, that condition might be always satisfiable because we can always assign characters arbitrarily: Given any string s that is consistent with the column multisets in the sense that for each column p, the character s[p] appears in C_p at least once? But we need to be able to fill the remaining n-1 rows with characters such that each column's multiset is exactly C_p. That is equivalent to saying that for each column p, the multiplicity of character s[p] in C_p must be at least 1, but also after removing one occurrence, the remaining multiset must be assignable to the other rows. But we can always assign any combination of characters as long as we can complete the assignment; but is there any restriction beyond that? Since we have n rows, for each column we have n characters (the multiset C_p). To assign a row 1 string s, we need that for each column p, we can remove one occurrence of s[p] from C_p, and the remaining n-1 characters can be arbitrarily assigned to the other rows (since no further constraints between columns). In principle, if we only require that the matrix's columns have specified multisets, then any choice of s with s[p] ∈ support(C_p) is possible? Not exactly: Suppose we have n=2, m=2. Column1: {A,B}, Column2: {A,B}. Then can we have row1 = "AA"? That would require taking A from column1 and A from column2. Then remaining characters for row2: column1 B, column2 B => "BB". So yes, matrix exists. Could we have row1 = "AB"? Then row2 gets "BA". Works. So all four combos possible. So indeed any string whose characters are in the support per column is possible? But what if some column has limited multiplicities? For example, n=2, m=2. Column1: {A,A}, Column2: {B,B}. Then only possible strings are "AB"? Because if row1 takes 'A' from col1 and 'B' from col2, row2 gets the remaining 'A' and 'B' => "AB" as well. So row1 can only be "AB". So not every combination is possible; we need that after choosing s, the remaining multiset per column can be paired arbitrarily? Actually there is no coupling between columns; we simply need that for each column p, we remove one occurrence of s[p] from the multiset C_p; the remaining multiset must be assignable to rows 2..n. But as long as the removal leaves a multiset of size n-1, it's automatically assignable to those rows because there is no further condition: we can assign the characters of each column independently to rows 2..n, and each row gets exactly one character per column. So after fixing row 1, we need to fill an (n-1) x m matrix such that column p has the given multiset (C_p minus s[p]). This is always possible because we can simply assign arbitrarily: list the n-1 rows, assign each column's characters to them in any order. Since there's no cross-column constraint, we can always do that. So the only condition is that for each column p, s[p] appears in C_p (i.e., count > 0). That seems sufficient to construct a matrix with given column multisets? Let's verify: We need to produce a matrix of n rows. We have row1 = s. For each column p, we have multiset C_p of size n. After removing s[p], we have a multiset of size n-1. We need to assign these characters to rows 2..n. This can be done by arbitrarily pairing each row with a character per column. Since each column's assignment is independent, we can fill the matrix. But is there any restriction that the rows must be distinct? No, rows can be identical; that's allowed. So indeed, any s such that for all p, s[p] is in the multiset of column p is constructible.
Thus, if the operations allow us to reach any matrix with those column multisets, then the answer would be the product over columns of (size of support of C_p)? Wait, number of possible s = product over p of (number of distinct letters in column p). Because we can choose any letter from column p independently? But careful: The selection of s must satisfy that for each column p, the letter s[p] appears at least once in C_p. Since we can pick any letter from that column's multiset. So the number of possible strings for row1 would be the product over p of (the number of distinct letters appearing in column p). However, the example 1: n=2,m=3. Strings: AAB, BAA. Column1: A, B => 2 distinct. Column2: A, A => 1 distinct. Column3: B, A => 2 distinct. Product = 212 = 4. That matches output 4. So seems plausible.
Now test example 2: Input:
4 5
ABABA
BCGDG
AAAAA
YABSA
Let's compute column multisets:
Row1: A B A B A
Row2: B C G D G
Row3: A A A A A
Row4: Y A B S A
So:
col1: A, B, A, Y => distinct: A, B, Y -> 3
col2: B, C, A, A => distinct: A, B, C -> 3
col3: A, G, A, B => distinct: A, B, G -> 3
col4: B, D, A, S => distinct: A, B, D, S -> 4
col5: A, G, A, A => distinct: A, G -> 2
Product = 33342 = 216. That matches output 216.
So indeed, answer equals product over columns of number of distinct characters in that column.
But we must verify that this condition is not only sufficient but also necessary? Could there be strings that are reachable but not satisfying that condition? Since column multisets are invariant, any reachable configuration must have exactly those column multisets. Hence, the string at row1 must have each character belonging to the column multiset. So it's necessary.
Now, is it true that we can achieve any such string? That is, can we achieve any matrix with the same column multisets? If yes, then the answer is as above. So we need to argue that the group generated by prefix swaps acts transitively on the set of matrices with given column multisets. Or at least, for any string s with s[p] ∈ C_p, there exists a sequence of prefix swaps that results in row1 being s, while leaving other rows arbitrary but consistent with column multisets.
We have an empirical match with examples, but we need to be sure. Since constraints are small, maybe the intended solution is exactly this product. But we need to ensure there is no hidden invariant beyond column multisets.
Let's explore invariants.
Consider the operation: swapping prefixes of length k between i and j. This is essentially a simultaneous transposition on the first k columns. By composing such operations, can we achieve any permutation of rows per column individually? Perhaps we can. Let's think about constructing any target matrix.
We have initial matrix A (n x m). Target matrix B with same column multisets. We want to transform A to B via prefix swaps.
We can attempt to build B from left to right. For column 1, we need to arrange the characters to match B's first column. Since we can swap prefixes of length 1 (k=1) between any two rows, we can permute rows arbitrarily in column 1. Indeed, swapping prefixes of length 1 swaps only the first character. So with such swaps, we can reorder the first column arbitrarily. So we can transform A such that column 1 equals B's column 1.
Now after setting column 1, we move to column 2. We need to set column 2 to match B's column 2, but we must be careful not to disturb column 1. However, we can use swaps with k=2 to adjust column 2 while possibly affecting column 1. But maybe we can use a strategy: Since we can already permute rows arbitrarily via whole-string swaps? Wait, swapping whole strings (k=m) would disturb everything. So we need a method to adjust later columns without messing up earlier ones.
Observation: If we fix the first column, we can still permute rows based on longer prefixes while preserving the first column? Not directly: swapping prefixes of length k (k>=2) swaps first k columns, so it would also swap the first column entries between rows. That would disturb column1 if we try to adjust column2.
But perhaps we can use a two-row operation that swaps only the second column while leaving the first column unchanged, by performing a combination of swaps. For instance, if we want to swap the characters in column 2 between rows i and j while leaving column 1 unchanged, can we do that? Let's try: Suppose we have rows i and j. We want to exchange only column2 entries. The current values at (i,1),(i,2) and (j,1),(j,2). If we swap prefixes of length 1, we swap column1 only; not helpful. If we swap prefixes of length 2, we swap both columns. That would exchange both column1 and column2, which would disturb column1 if they differ. But maybe we can first make column1 values equal between the two rows, then swap length2, then restore column1? Since we can modify other rows, maybe we can simulate any column permutation while keeping earlier columns invariant.
This suggests that the group may be large enough to achieve any configuration. The known result: The set of reachable matrices under prefix swaps between rows is exactly those matrices with the same column multisets. This is reminiscent of the "sorting by prefix reversals" or "parallel assignment". I recall a known problem: "Swapping prefixes" operation on a set of strings: The invariant is only the multiset of characters per column. And indeed, you can transform any configuration into any other with same column multisets. This is akin to a property of the "symmetric group acting on columns" with the ability to permute rows via operations that are transpositions on initial segments. Might be proven by induction on column index.
Let's attempt a constructive proof.
We have n rows, m columns. Operation: choose i, j, k, swap A[i,1..k] ↔ A[j,1..k].
Goal: Given two matrices A and B with same column multisets, transform A to B.
Proof by induction on columns: We'll transform column by column from left to right.
Base: For column 1, we can achieve any permutation of the entries among rows because swapping with k=1 allows swapping any two entries in column1. Since we can swap any two rows' first character via a direct operation (i,j,1). So we can reorder column1 arbitrarily. Thus, we can make column1 of A match column1 of B (by performing appropriate swaps). So after some operations, column1 matches B.
Now assume we have transformed A such that columns 1..c-1 match B. We need to adjust column c to match B without disturbing columns 1..c-1.
Now consider column c. The current entries in column c (after previous steps) may not match B's column c, but the multiset is correct (since column multiset invariant and we only permuted rows within first c-1 columns? But did we change column c in previous steps? Possibly yes, when we swapped prefixes of length >= c, column c might have been swapped. However, we have only used operations to adjust earlier columns; those operations may have affected column c as well. But we can plan to adjust column c later. The induction step: We need to achieve column c matching B while preserving columns 1..c-1.
Observation: We can perform operations that swap prefixes of length exactly c. This swaps the first c columns. If we swap rows i and j with k=c, then columns 1..c are swapped. This would disturb columns 1..c-1, which we already set. However, we might be able to use a combination that only permutes column c while leaving 1..c-1 unchanged, by using rows that currently have identical prefixes of length c-1.
Specifically, if two rows i and j have exactly the same characters in columns 1..c-1, then swapping their prefixes of length c will swap the entire first c columns, but since their first c-1 columns are identical, swapping them leaves those columns unchanged (as they are swapped with identical values). The only effect is that column c entries of those two rows are swapped. So if we can find pairs of rows with identical prefix of length c-1, we can permute column c entries among those rows arbitrarily (by swapping) while preserving columns 1..c-1.
Thus, the ability to permute column c while preserving earlier columns is contingent upon having enough rows with identical prefixes. But after we have matched columns 1..c-1 to B, the prefixes of length c-1 are exactly those from B. So the distribution of prefixes among rows is determined by B's first c-1 columns. In general, there might be rows with identical prefixes; we can use those to swap column c. But what if for a particular prefix pattern, there is only one row? Then how can we move a needed character into that row without disturbing the prefix? Possibly we need to involve more rows and use a sequence of swaps that temporarily alter prefixes but then restore them.
This is reminiscent of the fact that the group acts as the full symmetric group on each column individually when we consider the whole set, but with the ability to use other rows as "buffers". Since n can be up to 100, we can assume we have enough rows to facilitate swaps.
We need to show that for any target column c arrangement, we can achieve it without breaking earlier columns. There is a known lemma: The set of reachable configurations with fixed column multisets is exactly those configurations that are "consistent" under some equivalence. But the examples indicate product of distinct counts works, which would follow from full freedom. So I'm leaning that the intended solution is indeed product of distinct per column.
But we should double-check with potential counterexample.
Consider n=2, m=2. Strings: "AB", "BA". Column1: A,B distinct=2; column2: B,A distinct=2. Product=4. But is it possible to get "AA" at position1? Let's check: Starting from AB and BA. Column1: {A,B}; column2: {B,A}. To get row1=AA, need A from col1 and A from col2. But col2 has A and B; yes, there is one A. So we claim we can achieve. Let's try: operation: swap prefixes of length 1? That swaps first chars: rows become: BB? Actually swap first char: row1 becomes B B? Wait compute: row1: A B; row2: B A. Swap prefix length1: row1 becomes B B (first char from row2) but second char stays B? Actually after swap: row1's first becomes B (from row2), row1 second remains B (original) => row1 = B B? No original row1 second is B, yes so row1 becomes B B. row2 becomes A A. So we get row2 = AA. Then swap whole strings (k=2) to bring AA to row1. So yes.
Now consider n=2, m=2. Strings: "AA", "BB". Column1: A,B distinct=2; column2: A,B distinct=2; product=4. Can we get "AB" at row1? Starting: AA, BB. Need AB. Try: swap prefix length 1: row1 becomes B A? After swap: row1: B A (first char from row2 B, second char stays A) => BA; row2: A B. So we have BA and AB. So row2 is AB. Swap whole strings -> row1 AB. So yes.
Now consider a more tricky case: n=3, m=3. Strings: "ABC", "DEF", "GHI". Column distinct each 3 => product 27. But can we get any string of length 3 using letters from each column? For example, "AEI" where column1 A, col2 E, col3 I. Is that reachable? Let's try to reason: We need to assemble characters from three different original rows. Likely yes.
But what about a case where some column has only one distinct character? Then that column is forced to that character. So product accounts.
Now think about a scenario where column multisets have duplicates, but maybe there is a hidden "prefix" invariant that restricts combinations across columns beyond just per-column. For instance, could there be an invariant like the multiset of rows sorted lexicographically? But we saw we can create new strings not originally present, so that's not invariant.
Consider n=2, m=3. Strings: "ABA", "BAB". Column1: A,B distinct=2; column2: B,A distinct=2; column3: A,B distinct=2. Product=8. But are all 8 combos reachable? Let's test "AAA": need A from col1, A from col2, A from col3. col2 originally has B and A; yes has A. col3 originally has A and B; yes has A. So we claim reachable. Let's try to find sequence: initial: row1=ABA, row2=BAB. Want row1=AAA. Consider steps: swap prefix length 2? row1 first two from row2: row1 becomes BAB? Actually swap first two: row1: BA? Wait compute: row1= A B A; row2= B A B. Swap first 2: row1 becomes B A A? Actually row1 gets first two from row2: B A, plus third A (unchanged) => B A A => "BAA". row2 gets A B from row1 plus third B => A B B => "ABB". Not AAA. Swap whole strings: get row1= BAB? Not. Another: swap prefix length1: row1 becomes B B A? Actually swap first char: row1 becomes B B A? row1: first char from row2 (B) + rest "BA"? Actually row1: A B A -> after swap first char: row1 becomes B B A (since first becomes B, second stays B? Wait original row1 second is B, so row1 becomes B B A = "BBA". row2 becomes A A B = "AAB". Now swap prefix length 2 between row1 and row2? row1= B B A, row2= A A B. Swap first2: row1 becomes A A A? row1 gets A A from row2 plus third A? Actually third of row1 is A unchanged? Wait after swap, row1 becomes A A (from row2) and then third stays A? So row1 = "AAA". row2 becomes B B B? Actually row2 gets B B from row1 plus third B => "BBB". So yes we get AAA. So seems plausible.
Now consider a more complex invariant: For any prefix length p, the multiset of prefixes of length p across rows might be invariant? Let's test: Initially we have n strings. After swapping prefixes of length k between i and j, what happens to the multiset of length-p prefixes? For p ≤ k, swapping prefixes of length k will swap the entire length-p prefixes between i and j. So the multiset of length-p prefixes remains unchanged (since we are swapping two items). For p > k, swapping prefixes of length k does not affect the first p characters? Actually for p > k, the length-p prefix consists of the first k characters (which are swapped) and the next p-k characters (which are not swapped). After swapping the first k, the length-p prefixes of rows i and j become mixtures: row i's prefix becomes (row j's first k) + (row i's next p-k); row j's prefix becomes (row i's first k) + (row j's next p-k). So the multiset of length-p prefixes may change because these concatenations can create new prefixes not originally present. For example, with n=2, m=3, swapping k=2 changed prefixes of length 3 (full strings) obviously. So the multiset of full strings changes. But what about the multiset of prefixes of length p where p < m? In the example: initial prefixes length 2: {"AA","BA"}? Actually strings "AAB" and "BAA": prefixes length2: "AA" and "BA". After swapping k=2, strings become "BAB" and "AAB"? Wait we computed earlier: after swap first2, we got row1: "BAB"? Let's recalc: "AAB" and "BAA". Swap first2: row1 becomes "BAA"? Actually row1 gets first2 from row2: "BA", plus third from row1: "B" => "BAB"? row1 third originally is "B", yes => "BAB". row2 gets first2 from row1: "AA", plus third from row2: "A" => "AAA". So after swap, strings: "BAB", "AAA". Prefixes length2: "BA" and "AA". Original multiset of length2 prefixes: {"AA","BA"} (same multiset). So multiset of length2 prefixes remained? Indeed, after swap, we have "BA" and "AA" again. That's interesting. Let's test a different swap: initial "ABC","DEF". swap prefix length1: rows become "DBC","AEF". length1 prefixes: after swap, first chars: D and A; original first chars: A and D. Multiset: {A,D} same. length2 prefixes: For row1: "DB", row2: "AE". Original: "AB","DE". Are multisets same? Original: {"AB","DE"}. After: {"DB","AE"}. Are they equal as multisets? "AB" vs "DB" and "DE" vs "AE". Not obviously same. Let's check: original multiset of length2 prefixes: { "AB", "DE" }. After: { "DB", "AE" }. Are these the same? Possibly not. Example: letters: A,B,D,E. "AB" and "DE" vs "DB" and "AE". As multisets, they are different unless some coincidence. So multiset of length2 prefixes may change. But is there an invariant regarding the multiset of suffixes? Let's test: length2 suffixes (positions 2-3) maybe? Not sure.
Given that we have counterexample: For "ABC","DEF", after swapping first char, length2 prefixes changed. So no invariant for prefixes of length>k? But our product rule still works. So likely the only invariant is per column multiset.
Now we need to ensure that any assignment consistent with column multisets is reachable. This is akin to the matrix being "transformable" by a series of prefix swaps.
I recall a known result: The set of operations "swap prefixes of any length" generates the entire group of permutations that preserve column multisets. This is because these operations correspond to the action of the symmetric group on each column in a way that is "compatible" with the ability to sort rows. Actually, we can simulate any transposition of two entries in a specific column using a sequence of prefix swaps without affecting other columns? Let's try to design a procedure to swap two entries in column c while leaving all other columns unchanged.
Suppose we want to swap A[i,c] and A[j,c] but keep all other entries unchanged. Let's attempt to do it using prefix swaps. Let’s denote the rows as i and j. The idea: First, make the prefixes of length c-1 of rows i and j identical (maybe by swapping with other rows or by modifying them). If they are already identical, then swapping prefixes of length c will swap the first c columns, but since the first c-1 are identical, they remain unchanged, and column c entries get swapped. So that achieves the swap of column c alone. So the difficulty is if the prefixes of length c-1 differ. We need to make them identical temporarily, do the swap, then restore. We can use a third row as a buffer. Let’s outline:
We have rows i, j, and maybe a third row t. We want to swap A[i,c] and A[j,c]. Steps:
Find a row t such that its prefix of length c-1 equals either that of i or j (or we can modify t). But we need to ensure we can later restore.
Alternative approach: Use a series of prefix swaps to permute entries such that eventually we can achieve the desired swap. This resembles the fact that the group generated is the full group of column-preserving permutations? Actually, consider the matrix as a set of rows; we can think of each row as a tuple. The operation swaps prefixes. The group might be isomorphic to the wreath product of symmetric groups? But let's not overcomplicate.
Given the constraints are small (n,m up to 100) and the answer for examples matches product of distinct counts, it's likely that the intended solution is indeed that product, and the reasoning is that the only invariant is the multiset per column, and one can prove reachability (maybe by constructing a sorting algorithm: we can transform any configuration into a canonical one, and then from that to any other). For programming contests, often such problems are solved by noting that we can rearrange each column arbitrarily because we can swap any two entries in a column using the following method (found in editorial for similar problem?).
I recall a Codeforces problem "Little Pony and Sort by Shift"? Not that. There's a known CF problem: "Swapping Prefixes" maybe. Let me search memory: There's a problem "Vasya and String" no. But I think it's CF Round #... Actually, this problem might be from Codeforces: "Vasya and Magic Formula" or something. I'm not sure. But the pattern of answer being product of distinct letters per column is striking.
Let's attempt to construct a rigorous proof for reachability.
Claim: Given any two matrices A and B with the same column multisets, there exists a finite sequence of allowed operations turning A into B.
Proof by construction: We'll transform A into B column by column from left to right. Assume we have processed columns 1..c-1 to match B. Now we need to fix column c.
Let P be the set of rows. For each row, the first c-1 columns already match B. So for any row i, the prefix of length c-1 is exactly some string X_i (which is the target prefix from B). In B, for rows with same prefix, the column c entries are some multiset. Our current column c entries (call current values d_i) might not match the target values t_i. However, the multiset of d_i equals the multiset of t_i because both matrices have same column multiset for column c, and earlier columns are same as B so the distribution of prefixes may affect something? Wait: The column c multiset is global across all rows, independent of prefixes. So the multiset of d_i equals multiset of t_i.
Now we need to permute the column c entries among rows so that each row gets its target t_i, while keeping prefixes unchanged. The operation allowed: swapping prefixes of length c between two rows. If two rows have identical prefixes of length c-1, then swapping their prefixes of length c will swap their column c entries while leaving prefixes unchanged (since the first c-1 are identical). So if we can pair up rows that need to exchange column c values and they have identical prefixes, we can do it. But what if a row i needs a value that is currently in row j, but their prefixes differ? Then we cannot directly swap because that would disturb prefixes.
We can use a third row as a buffer. Since n can be up to 100, we can always find a row with the same prefix as one of them, or we can temporarily change a row's prefix to facilitate swap and then restore.
Idea: We can treat the rows with same prefix as groups. Within each group, we can freely permute column c entries via swaps of length c (since prefixes identical). So if we can get the required target values into the correct groups, we can then permute within groups. So the problem reduces to moving column c entries between groups without altering prefixes. How to move an entry from a row in group G1 to a row in group G2?
To move a value v from row i (group G1) to row j (group G2), while keeping prefixes intact, we can use a row k in group G1 (or G2) as a buffer. Suppose we want to put v into row j. We can first swap column c entries between i and some row k in group G1 (using length c swap) to bring v to k, but k is still in group G1. Then we need to transfer v from group G1 to group G2. We need an operation that moves v across groups without affecting prefixes. If we could swap prefixes of length c between k and j, but that would swap prefixes (first c-1) as well, which would break prefixes because G1 and G2 have different prefixes. However, if we temporarily change the prefix of row k to match G2, do the swap, then revert? But changing prefixes would break earlier columns.
Alternative: Use a series of swaps that involve a row that has a prefix that is a "bridge". Since all prefixes are strings of length c-1, we can consider a graph where vertices are distinct prefix strings. The operation of swapping prefixes of length c between two rows i and j effectively swaps their column c entries but also swaps their prefixes if the prefixes differ. So if we perform such a swap between rows from different groups, both prefixes are swapped. That would disturb the prefixes we have already set. However, if we later fix those prefixes by additional swaps, maybe we can achieve net effect of moving a column c entry between groups while ultimately restoring prefixes.
Let's consider a 3-cycle method: We want to transfer value v from row i (group G1) to row j (group G2), and move the value originally in row j (say u) to somewhere else. We could perform a cyclic permutation using a third row r that we are willing to temporarily sacrifice. For instance, suppose we have rows i (prefix P1, value v), j (prefix P2, value u), and r (prefix P3, value w). If we perform swap length c between i and r: then i gets prefix P3 and value w, r gets prefix P1 and value v. Then swap length c between r and j: now r (now with prefix P1, value v) swaps with j (prefix P2, u). After swap: r gets prefix P2, value u; j gets prefix P1, value v. Then swap length c between i (now P3, w) and r (now P2, u): after swap, i gets prefix P2, value u; r gets prefix P3, value w. Net effect: i: prefix P2, value u; j: prefix P1, value v; r: prefix P3, value w. So prefixes have been swapped between i and j, while column c values v and u have also swapped, but also the prefixes of i and j have been exchanged. That's not what we want: we want prefixes to remain as before (i should have P1, j should have P2). However, we could then swap the prefixes of i and j using a length c-1 swap? But swapping prefixes of length c-1 would change earlier columns, which we don't want to disturb. But maybe we can use a different scheme.
Maybe we don't need to keep prefixes fixed individually; we only need that at the end, each row's first c-1 columns match B. So we can allow temporary disturbances as long as we finally fix them. So we could first get column c correct even if prefixes are scrambled, and then re-adjust columns 1..c-1 using the induction step again? But that would break our induction order because after messing prefixes, we would need to re-fix them. But we could do a more careful process: first, we only care about column c final values. We could aim to achieve column c correct while maybe sacrificing earlier columns, then use the induction hypothesis (which says we can fix earlier columns without affecting column c? But does fixing earlier columns affect column c? If we use swaps of length less than c, they don't affect column c. Swaps of length exactly c affect column c as well. Swaps of length > c affect column c. So if we need to fix earlier columns without altering column c, we might restrict to swaps with length ≤ c-1, which don't affect column c. So after column c is set, we can re-fix columns 1..c-1 using swaps of length up to c-1 only. That might be possible because we already know we can achieve any permutation of columns 1..c-1 using such swaps? But earlier we argued that for column 1 we used k=1 swaps, for column 2 we might need to use k=2 swaps which affect column1. However, if we have already set column c, we don't want to use swaps of length >= c. But to adjust columns 1..c-1, maybe we can use only swaps of length up to c-1. Are swaps of length up to c-1 sufficient to achieve any permutation of the first c-1 columns? Let's examine.
We have a set of rows. We want to rearrange the first c-1 columns arbitrarily. The operations allowed: for any i,j and any k (1 ≤ k ≤ m). So we can use k = c-1 as well, which swaps the first c-1 columns. That would disturb column c-1, but that's within the set we are adjusting. But it would also disturb columns 1..c-2? Actually swapping prefixes of length c-1 swaps exactly the columns we want to adjust, so it's allowed because we are in the process of adjusting them. However, such swaps also affect column c? No, because k=c-1, so column c is not touched. So we can use swaps of length exactly c-1 to permute the first c-1 columns. However, the same issue arises: swapping length c-1 between two rows swaps all of columns 1..c-1, so it permutes those columns as a block. But we need more fine-grained control to set each column individually. But we previously argued that we can set column 1 using k=1 swaps. To set column 2 without affecting column 1, we used the trick that if two rows have identical column1, swapping length 2 doesn't affect column1. But after we have set column1 to target, we can then set column2: we can permute column2 entries among rows with same column1 value using length2 swaps (since identical column1, swapping length2 leaves column1 unchanged). Then we might need to move entries between different column1 groups. To move a value from one group to another, we might need to temporarily alter column1, but then fix it using swaps of length1. That might be okay as long as we don't disturb column c (which we haven't set yet). So the process is: we can set columns sequentially, and when setting column c, we may use swaps of length up to c, which might disturb earlier columns; but we can later re-fix earlier columns using swaps of length up to c-1, which do not affect column c because they don't touch column c (since k ≤ c-1). So it's plausible we can achieve any configuration.
Thus, a constructive algorithm: For each column from 1 to m, we can first arrange that column to match B, using any operations (including those that mess up previous columns). Then, after column m is done, we fix columns 1..m-1 using only operations with k ≤ m-1, which do not affect column m. But wait, fixing columns 1..m-1 might again disturb column m if we use k=m? We won't. So we can fix backwards? Actually easier: We can process columns from left to right, and after setting column c, we keep it fixed while setting later columns? But later columns may require swaps of length ≥ c+1, which would affect column c. So we can't keep it fixed. Alternatively, we could set columns from right to left using suffix swaps? But operation only swaps prefixes, not suffixes. But maybe we can simulate suffix swaps by combining with whole swaps? Hmm.
Better approach: Instead of trying to keep earlier columns fixed, we can bring the whole matrix to a canonical form, e.g., sort rows lexicographically. Since column multisets are invariant, if we can show that from any configuration we can reach the lexicographically sorted order (or some specific order), then the set of reachable configurations is the orbit, and since the column multisets are the only invariant, the orbit is all matrices with those column multisets. But need to prove that the group action is transitive on the set of matrices with given column multisets.
Another way: Model the matrix as a set of n rows; we want to show that for any two rows, we can swap their characters at a single column while leaving other columns unchanged, using a sequence of prefix swaps. If we can achieve any transposition on a specific column, then we can permute that column arbitrarily, and then with composition we can achieve any matrix. So we need to show: For any i, j, and column c, there exists a sequence of allowed operations that swaps A[i,c] and A[j,c] and leaves all other entries unchanged. If we can do this, then we can independently permute each column to achieve any target configuration, because we can then rearrange column by column.
So let's try to construct such a "elementary column swap" operation.
We have rows i and j. We want to swap their entries in column c only. Let's attempt to do it with three rows, including possibly using a temporary row.
Idea: Use row t as a buffer with a distinct prefix that we don't care about. However, we must ensure that after the sequence, all rows except maybe i and j return to original state.
Consider rows i, j, and t (t distinct from i,j). Suppose we perform:
Swap prefixes of length c between i and t. (Now i has prefix of t, and t has prefix of i; column c entries swapped as well.)
Swap prefixes of length c between t and j. (Now t has prefix of i and column c entry from j? Let's track carefully.)
We need to track state of rows i, j, t across steps. Let's denote:
Initially:
Row i: prefix P_i (length c-1), char a_i at col c, suffix (rest).
Row j: prefix P_j, char a_j.
Row t: prefix P_t, char a_t.
Goal: swap a_i and a_j, leaving prefixes P_i, P_j unchanged, and row t unchanged.
Operation 1: swap length c between i and t.
After step1:
i: prefix P_t, char a_t, suffix_i unchanged? Actually swapping length c swaps first c columns, so i gets first c of t: prefix P_t and char a_t; and retains its suffix (c+1..m) unchanged? Wait, swapping prefixes of length c exchanges exactly the first c characters. So the suffix (c+1..m) stays with the original row. So i's suffix remains from original i; similarly t's suffix remains from original t. So after step1:
i: (P_t, a_t) + suffix_i
t: (P_i, a_i) + suffix_t
j: unchanged.
Step2: swap length c between t (now with prefix P_i, a_i) and j (prefix P_j, a_j). After swap:
t: gets first c from j: (P_j, a_j) + suffix_t (original t suffix)
j: gets first c from t (which was (P_i, a_i)) + suffix_j
So after step2:
i: (P_t, a_t) + suffix_i
j: (P_i, a_i) + suffix_j
t: (P_j, a_j) + suffix_t
Now we have j has the desired char a_i but its prefix is P_i, not P_j. i has char a_t, not desired. t has char a_j and prefix P_j.
We need to restore prefixes. We can swap length c between i and t again? Step3: swap length c between i (P_t, a_t) and t (P_j, a_j). After swap:
i: gets (P_j, a_j) + suffix_i
t: gets (P_t, a_t) + suffix_t
j: unchanged (P_i, a_i).
Now i has char a_j and prefix P_j; j has char a_i and prefix P_i; t is back to its original (P_t, a_t) + suffix_t? t now has (P_t, a_t) and its original suffix, yes. So after three swaps, i and j have swapped their column c entries, but also their prefixes have been swapped! Because i now has prefix P_j, and j has prefix P_i. So the net effect is that i and j swapped both their prefixes (first c-1 columns) and column c entries. That's a full swap of the first c columns between i and j. That's not what we wanted; we wanted only column c swapped while prefixes unchanged.
To achieve only column c swap, we need to swap the prefixes back without affecting column c. But swapping prefixes back would require swapping length c-1 between i and j. However, swapping length c-1 swaps the first c-1 columns, which would swap the prefixes but also leave column c unchanged? Actually swapping length c-1 does not affect column c. So after step3, we could swap length c-1 between i and j. That would swap their prefixes back, while leaving column c entries unchanged (since column c not included). Let's see: After step3, i: (P_j, a_j) + suffix_i; j: (P_i, a_i) + suffix_j. Swap length c-1 between i and j: This swaps the first c-1 columns. So after swap:
i: prefix P_i, but column c remains a_j? Wait careful: Swapping length c-1 means we exchange the first c-1 characters only. Column c is not swapped. So i gets prefix P_i from j, and keeps its own column c (a_j) and suffix. j gets prefix P_j from i, and keeps its own column c (a_i) and suffix. So after step4:
i: prefix P_i, column c = a_j, suffix_i
j: prefix P_j, column c = a_i, suffix_j
That's exactly the desired state! Row t returned to original. All other rows unchanged. So we have constructed a sequence of operations that swaps column c entries of rows i and j while leaving everything else unchanged, using an auxiliary row t.
We need to ensure that the operations are allowed: all swaps are between distinct rows (i,t), (t,j), (i,t), (i,j) with appropriate k values. The last swap uses k=c-1. This is allowed as long as c-1 ≥ 1. For c=1, column 1 swap is just swapping first character; we can do that directly with k=1. So for c>=2, we have a procedure using 4 swaps. So indeed, we can perform any transposition of entries in a specific column while leaving other columns unchanged. This is sufficient to show that we can arbitrarily permute the entries within each column independently.
Thus, the set of reachable configurations is exactly those matrices with the same column multisets, because we can independently permute each column's entries to match any desired assignment consistent with those multisets. Moreover, the procedure to swap two entries in column c uses an auxiliary row that must be distinct from i and j. With n ≥ 3, we have at least one auxiliary row. What about n=2? For n=2, we cannot use a third row. But we can still perform column swaps? Let's test: n=2, m arbitrary. We need to show we can achieve any configuration consistent with column multisets. For n=2, the column multiset per column has exactly two entries. The possible assignments for that column are either the original order or swapped. Can we achieve both? Yes, by swapping prefixes of length 1 (for column 1) or using a more complex sequence? For column c>1, can we swap column c entries without swapping prefixes? For n=2, there is no third row to buffer. However, we can possibly still achieve both orders. Let's test with n=2,m=2 example: we were able to get both orders for column2? Actually we were able to get row1=AA, which required swapping column2 entries? Let's see: Starting with (AA, BB) we got row1=AB? Actually from (AA,BB) we got (AB,BA). That involved swapping column2 entries? Initially column2: A,B. After operations, we got column2: B,A. So column2 got swapped. Did we achieve that by a direct swap of column2 entries? Let's see the sequence we used: swap prefix length1: rows become (BA, AB). That swapped column1 and also swapped column2? Actually swap length1 only swapped first character; column2 remained as original? Original column2: row1 A, row2 B. After swap length1: row1 becomes B (first char) + A (second char) -> BA, row2 becomes A + B -> AB. So column2 now: row1 A? Wait row1 second char is A (unchanged), row2 second char is B (unchanged). So column2 didn't swap; it stayed A,B. Then we swapped whole strings (k=2) to get (AB, BA). That swapped both columns, thus column2 swapped. So indeed we can swap column2 by swapping entire strings. But that also swaps column1. To get column2 swapped while keeping column1 fixed, we might need a more intricate sequence. But since we only care about the final configuration of whole rows, not necessarily independent per column, we might not need to swap column c alone; we can combine with other columns adjustments. For n=2, we can still achieve any assignment of column values because there are only two possible assignments per column: either original order or swapped. And both are reachable via swapping whole strings (k=m) which swaps all columns. So yes, we can achieve both orders. So for n=2, it's still true that any matrix with same column multisets is reachable? Let's test a scenario: n=2, m=2, column multisets: col1 {A,B}, col2 {A,B}. The possible matrices are four: (AA,BB), (AB,BA), (BA,AB), (BB,AA). Are all reachable from, say, initial (AA,BB)? We already got (AB,BA) via swap length1 then swap whole? Let's try to get (BA,AB). From (AA,BB), swap whole strings gives (BB,AA). Not (BA,AB). How to get (BA,AB)? Starting from (AA,BB): we could swap length1 to get (BA,AB)?? Let's compute: swap prefix length1: (BA, AB) yields (BA,AB). Indeed, that directly gives (BA,AB). So from (AA,BB), we can get (BA,AB) with one operation (k=1). So we have (AA,BB) -> (BA,AB). Then from (BA,AB) we can swap whole strings to get (AB,BA). So all four are reachable. So yes.
Thus, the existence of the auxiliary row for c≥2 when n≥3 ensures we can perform arbitrary permutations per column. For n=2, we can handle directly (since only two rows, the only permutations are swap or identity, both achievable). So overall, any configuration with same column multisets is reachable.
Therefore, the set of possible strings for row1 is exactly all strings s of length m such that for each column p, s[p] belongs to the set of letters present in column p (i.e., the support of C_p). And these choices are independent across columns (since we can realize any combination via appropriate permutation of each column). Hence, number of possibilities = product over p of (number of distinct letters in column p). Mod modulo 1e9+7.
That is the answer.
Now we must ensure that there is no additional constraint like "the final configuration must be reachable without changing the multiset of strings?" No, we already argued.
Double-check edge cases:
n=1: Only one name. Then operations? i,j require i<j so with n=1, no operations possible. So the only possible name is the original one. Our method: For each column, number of distinct letters = 1 (since only one string). Product = 1. So matches.
m=1: Then operation swaps prefixes of length 1, which swaps the only character between two strings. So we can permute the characters arbitrarily among rows. The column multiset is fixed. So possible strings for row1 are any character that appears in that column. Number = number of distinct letters among all strings. That's product over columns (only one column) = distinct count. Good.
Strings may have uppercase letters only, so up to 26 distinct per column.
Thus, solution: read n, m, read n strings. For each column j (0-indexed), compute the set of characters appearing in that column among all strings. Multiply the sizes of these sets modulo 1e9+7.
Complexity: O(n*m) = up to 10^4, trivial.
Now, we should also consider potential pitfalls: The modulo is prime but product may be large, use mod multiplication.
Implementation steps:
Input n, m.
Create a list of n strings.
Initialize ans = 1.
For col in range(m):
Create a boolean array or set of characters seen.
For each row in range(n): add string[row][col] to set.
Multiply ans by len(set) % MOD.
Print ans.
That's it.
But ensure that we consider the possibility that some column might have 0 distinct letters? Not possible because n≥1 and each string length m, so each column has exactly n characters, so at least 1 distinct.
Edge: When m=0? Not possible because m ≥ 1.
Thus, solution is straightforward.
Now, we need to produce final answer.
We'll also include reasoning in comments for thoroughness.
Thus, final output: the product modulo 1e9+7.
Let's double-check with given examples:
Example1: column distinct counts: 2,1,2 => 4 mod.
Example2: 3,3,3,4,2 => 216.
Matches.
Thus, problem solved.
Now, I'll write the final answer with explanation. ```python
import sys
MOD = 1000000007
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
names = [next(it).strip() for _ in range(n)]
code
ans = 1
for col in range(m):
seen = set()
for row in range(n):
seen.add(names[row][col])
ans = (ans * len(seen)) % MOD
print(ans)