Intellectual Inquiry
Contest: CROC 2016 - Elimination Round
Difficulty rating: 2200
Tags: dp, greedy, strings

After getting kicked out of her reporting job for not knowing the alphabet, Bessie has decided to attend school at the Fillet and Eggs Eater Academy. She has been making good progress with her studies and now knows the first k English letters.

Each morning, Bessie travels to school along a sidewalk consisting of m + n tiles. In order to help Bessie review, Mr. Moozing has labeled each of the first m sidewalk tiles with one of the first k lowercase English letters, spelling out a string t. Mr. Moozing, impressed by Bessie's extensive knowledge of farm animals, plans to let her finish labeling the last n tiles of the sidewalk by herself.

Consider the resulting string s (|s| = m + n) consisting of letters labeled on tiles in order from home to school. For any sequence of indices p1 < p2 < ... < pq we can define subsequence of the string s as string sp1sp2... spq. Two subsequences are considered to be distinct if they differ as strings. Bessie wants to label the remaining part of the sidewalk such that the number of distinct subsequences of tiles is maximum possible. However, since Bessie hasn't even finished learning the alphabet, she needs your help!

Note that empty subsequence also counts.

Input format:
The first line of the input contains two integers n and k (0 ≤ n ≤ 1 000 000, 1 ≤ k ≤ 26).

The second line contains a string t (|t| = m, 1 ≤ m ≤ 1 000 000) consisting of only first k lowercase English letters.

Output format:
Determine the maximum number of distinct subsequences Bessie can form after labeling the last n sidewalk tiles each with one of the first k lowercase English letters. Since this number can be rather large, you should print it modulo 109 + 7.

Please note, that you are not asked to maximize the remainder modulo 109 + 7! The goal is to maximize the initial value and then print the remainder.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first sample, the optimal labeling gives 8 different subsequences: "" (the empty string), "a", "c", "b", "ac", "ab", "cb", and "acb".

In the second sample, the entire sidewalk is already labeled. The are 10 possible different subsequences: "" (the empty string), "a", "b", "aa", "ab", "ba", "aaa", "aab", "aba", and "aaba". Note that some strings, including "aa", can be obtained with multiple sequences of tiles, but are only counted once.

Example input:
1 3
ac

Example output:
8

Example input:
0 2
aaba

Example output:
10

Editorial:
Idea: [user:yummy]
For simplicity, let our letters be 1, 2, ..., k, instead of actual characters. Let s[i] denote the number of distinct subsequences that end in the letter i. If you append the letter j to a sequence, the only change to the s sequence is $$s[j] \rightarrow \sum_{i=1}^{k} s[i] + 1.$$ This is because we can take any string that ends in any of the letters 1, 2, ..., k, and append a j to it, or just add the single letter string j. It isn't hard to see why this works.
The key observation is that no matter what character j we choose to append, s[j], will always end up the same: $$\sum s[i] + 1.$$ Therefore, we should greedily choose the character j with minimum s[j] to append. The implementation now seems easy, except we somehow need to maintain it $$\mod 10^9+7$$, which doesn't go well with minimum.
The final observation is that if the last occurrence of j is after the last occurrence of j' in our string, then s[j] > s[j'], by analyzing the transition. So our algorithm instead should not choose the minimum, instead choose the letter that appeared least recently.
Implementing this now takes O(n + L) time.