62 lines
1.1 KiB
Python
62 lines
1.1 KiB
Python
MOD = 10 ** 9 + 7
|
|
|
|
|
|
def factorials(n, c):
|
|
l = [1]
|
|
for x in range(c + 1, n):
|
|
l.append((l[-1] * x) % MOD)
|
|
return l
|
|
|
|
|
|
N, K = map(int, input().split())
|
|
|
|
alph = []
|
|
for _ in range(N):
|
|
inp = input().strip()
|
|
alph.append(inp)
|
|
alph = sorted(alph)
|
|
|
|
fac = factorials(len(alph), N - K)
|
|
|
|
TREE = {}
|
|
for ind, init in enumerate(alph):
|
|
tree_ = TREE
|
|
i = 0
|
|
while init[i] in tree_ and isinstance(tree_[init[i]], dict):
|
|
tree_ = tree_[init[i]]
|
|
i += 1
|
|
substr = init[i + 1:-1]
|
|
subtree = {}
|
|
if substr:
|
|
st_ = subtree
|
|
for c in substr:
|
|
st_[c] = {}
|
|
st_ = st_[c]
|
|
st_[init[-1]] = ind
|
|
else:
|
|
subtree = ind
|
|
tree_[init[i]] = subtree
|
|
|
|
|
|
def get_blocks(s):
|
|
tree_ = TREE
|
|
for c in s:
|
|
if isinstance(tree_, dict):
|
|
tree_ = tree_[c]
|
|
else:
|
|
yield tree_
|
|
tree_ = TREE[c]
|
|
yield tree_
|
|
|
|
|
|
inds = list(get_blocks(input().strip()))
|
|
|
|
index = 0
|
|
indexes = list(range(len(alph)))
|
|
for j, i in enumerate(inds):
|
|
k = indexes.index(i)
|
|
del indexes[k]
|
|
index += (fac[K - j - 1] * k) % MOD
|
|
|
|
print(index + 1)
|