#P12238. Prefix Partitioning in LQ Country

    ID: 14344 Type: Default 1000ms 256MiB

Prefix Partitioning in LQ Country

Prefix Partitioning in LQ Country

In the distant LQ country, every word is composed exclusively of the three characters l\tt{l}, q\tt{q} and b\tt{b} (with ASCII codes 108108, 113113, and 9898, respectively). A magic dictionary contains NN words. In order to help memorize words faster, Xiaolan decides to partition the dictionary into KK classes by choosing KK distinct non‐empty prefixes with the following requirements:

  1. Choose KK different non‐empty prefixes that occur in the dictionary (each chosen prefix must be a prefix of at least one word).
  2. For every word in the dictionary, there must be exactly one chosen prefix that is a prefix of that word. (In other words, the KK chosen prefixes form a prefix–free set that covers every word exactly once.)
  3. Each of the KK classes (i.e. the set of words having the same chosen prefix) is non–empty.

Your task is to count the number of ways to choose such a set of KK prefixes. Two ways are considered different if the sets of chosen prefixes differ. Since the answer can be very large, output it modulo 109+710^9+7.

Formally, given a dictionary of NN words (each word a string over the alphabet {l, q, b}) and an integer KK, count the number of sets PP of KK non–empty strings satisfying:

  • For every pPp\in P, there exists at least one word in the dictionary with prefix pp.
  • For every word ww in the dictionary, there is exactly one pPp\in P such that pp is a prefix of ww.

Note that we do not allow the empty string as a prefix.

inputFormat

The first line contains two integers NN and KK (1N1051 \le N \le 10^5, 1KN1 \le K \le N), representing the number of words in the dictionary and the required number of classes, respectively. Each of the next NN lines contains a non–empty string consisting only of the characters 'l', 'q' and 'b'. It is guaranteed that the total length of all words does not exceed 10510^5.

outputFormat

Output a single integer: the number of ways to choose KK prefixes to partition the dictionary as described, modulo 109+710^9+7.

sample

1 1
l
1

</p>