J. The Zong of the Zee

Codeforces
IDCF10212J
Time3000ms
Memory512MB
Difficulty
English · Original
Formal · Original
Imperturbable Captain of the Unterzee chases the Pirate-Poet. Their rivalry was set long ago and since then knows no limits. Every time the Captain arrives at the port where the Pirate-Poet was expected to be, they find nothing but the message from the Pirate-Poet acknowledging that she departed the port a few days ago and leaving the Captain with another line of never-ending masterpiece: the Zong of the Zee. Both the Captain and the Pirate-Poet know that the end of the Zong will put an ultimate end to their rivalry. Once the Captain retrieves it, they will find the Pirate-Poet and the final clash will begin. Port after port, line after line... Perhaps, the answer was there all along. The Captain knows they can't possibly last long enough to witness the end of the Zong, thus the only thing they can do is to outplay the Pirate-Poet. So far, every single line of the poem was an anagram of the first one. Moreover, the Captain suspects that all lines are obtained in the same way, namely by applying some fixed permutation $pi_1, \\dots, pi_n$ to the previous line. That is, the character on position $i$ on a new line was on position $pi_i$ on the previous one. The Captain wants to recover the whole poem by guessing the intended permutation. But firstly they want to know how many variants they have to check. The Captain thoroughly copied every single line of the poem, $m$ lines of $n$ Unterzian letters each. Unfortunately, it is sometimes hard to understand the Captain, thus some of letters (but at most one per line) may be replaced by question marks ("_?_"), meaning that you are uncertain what letter exactly it is. You have to calculate the number of permutations $pi_1, \\dots, pi_n$ which are consistent with the given data: in other words, such permutations that there is a way to replace all "_?_" with some Unterzian letters so that each line of the poem is obtained from the previous one by applying this permutation. Output the answer modulo $10^9 + 7$. The Captain knows what to do with this information. Probably. The first line of input contains two integers $m$ and $n$ ($2 <= n, m <= 3000$). The $i$-th of the following $m$ lines contains the $i$-th line of the poem. Each such line contains exactly $n$ characters, each character being either an Unterzian letter or a question mark ("_?_"). Each line contains at most one question mark. Each Unterzian letter has ASCII code between 33 and 126, except for "_?_" which has code 63. Output a single integer which is the answer to the problem. ## Input The first line of input contains two integers $m$ and $n$ ($2 <= n, m <= 3000$).The $i$-th of the following $m$ lines contains the $i$-th line of the poem. Each such line contains exactly $n$ characters, each character being either an Unterzian letter or a question mark ("_?_"). Each line contains at most one question mark.Each Unterzian letter has ASCII code between 33 and 126, except for "_?_" which has code 63. ## Output Output a single integer which is the answer to the problem. [samples]
**Definitions** Let $ m, n \in \mathbb{Z} $ with $ 2 \leq n, m \leq 3000 $. Let $ L_1, L_2, \dots, L_m $ be strings of length $ n $ over the alphabet $ \Sigma \cup \{?\} $, where $ \Sigma $ is the set of Unterzian letters (ASCII 33–126 excluding 63), and $ ? $ denotes an unknown character. Each $ L_i $ contains at most one $ ? $. Let $ \pi \in S_n $ be a permutation of $ \{1, 2, \dots, n\} $. **Constraints** For each $ i \in \{2, \dots, m\} $, there exists a string $ L_i' $ obtained from $ L_i $ by replacing its single $ ? $ (if any) with some letter in $ \Sigma $, such that: $$ \forall j \in \{1, \dots, n\}, \quad L_i'[j] = L_{i-1}'[\pi(j)] $$ where $ L_{i-1}' $ is similarly obtained from $ L_{i-1} $. **Objective** Count the number of permutations $ \pi \in S_n $ for which there exist valid replacements of all $ ? $ in $ L_1, \dots, L_m $ such that the above consistency condition holds for all $ i = 2, \dots, m $. Output the count modulo $ 10^9 + 7 $.
API Response (JSON)
{
  "problem": {
    "name": "J. The Zong of the Zee",
    "description": {
      "content": "Imperturbable Captain of the Unterzee chases the Pirate-Poet. Their rivalry was set long ago and since then knows no limits. Every time the Captain arrives at the port where the Pirate-Poet was expect",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 3000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10212J"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Imperturbable Captain of the Unterzee chases the Pirate-Poet. Their rivalry was set long ago and since then knows no limits. Every time the Captain arrives at the port where the Pirate-Poet was expect...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ m, n \\in \\mathbb{Z} $ with $ 2 \\leq n, m \\leq 3000 $.  \nLet $ L_1, L_2, \\dots, L_m $ be strings of length $ n $ over the alphabet $ \\Sigma \\cup \\{?\\} $, where $ \\Sigma $ is the...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments