Bacterial Melee
Contest: 8VC Venture Cup 2017 - Final Round
Difficulty rating: 2400
Tags: brute force, combinatorics, dp, string suffix structures

Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters "a", ..., "z".

The testtube is divided into n consecutive regions. Each region is occupied by a single colony of a certain bacteria type at any given moment. Hence, the population of the testtube at any moment can be described by a string of n Latin characters.

Sometimes a colony can decide to conquer another colony in one of the adjacent regions. When that happens, the attacked colony is immediately eliminated and replaced by a colony of the same type as the attacking colony, while the attacking colony keeps its type. Note that a colony can only attack its neighbours within the boundaries of the testtube. At any moment, at most one attack can take place.

For example, consider a testtube with population "babb". There are six options for an attack that may happen next:

- the first colony attacks the second colony (1 → 2), the resulting population is "bbbb";
- 2 → 1, the result is "aabb";
- 2 → 3, the result is "baab";
- 3 → 2, the result is "bbbb" (note that the result is the same as the first option);
- 3 → 4 or 4 → 3, the population does not change.

The pattern of attacks is rather unpredictable. Julia is now wondering how many different configurations of bacteria in the testtube she can obtain after a sequence of attacks takes place (it is possible that no attacks will happen at all). Since this number can be large, find it modulo 109 + 7.

Input format:
The first line contains an integer n — the number of regions in the testtube (1 ≤ n ≤ 5 000).

The second line contains n small Latin letters that describe the initial population of the testtube.

Output format:
Print one number — the answer to the problem modulo 109 + 7.

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

Note:
In the first sample the population can never change since all bacteria are of the same type.

In the second sample three configurations are possible: "ab" (no attacks), "aa" (the first colony conquers the second colony), and "bb" (the second colony conquers the first colony).

To get the answer for the third sample, note that more than one attack can happen.

Example input:
3
aaa

Example output:
1

Example input:
2
ab

Example output:
3

Editorial:
Hint: find a condition when a string is reachable from another string in terms of subsequences, then apply DP for counting suitable subsequences.
Solution: How to determine if a string can be obtained from another string after a number of operations? It helps to consider (maximal) blocks of adjacent characters. Let us define comp(s) as a "compressed" version of s that keeps a single character from each block of s in the same order, for example, $$comp(aaabbca) = abca$$. It is not hard to see that each operation either preserves the sequence of blocks (if we ignore their lengths) or erases a single block (when it consists of a single character and is overwritten by an adjacent character). This is the same as saying that each operation applied to s either doesn't change comp(s) or erases a single character from it. Therefore, if t can be obtained from s after a number of operations, then comp(t) must be a subsequence of comp(s).
In fact, this is an "if and only if" condition. Indeed, suppose that comp(t) is a subsequence of comp(s). We start our process of making t from s by eliminating the blocks not present in t, effectively making comp(s) = comp(t). After that, it can be seen that the borders of the blocks can be moved rather freely (but carefully enough not to murder any block) so that s can be indeed made equal to t.
We can see from the last argument that from the reachability point of view the two strings s and t are essentially different if comp(s) ≠ comp(t). If s is the given string, and a string u is a subsequence of comp(s) that doesn't have equal adjacent characters, then there are $${ \binom { n-1 } { | u | - 1 } }$$ reachable strings with comp(...) = u; indeed, this is exactly the number of ways to split n characters into |u| non-empty blocks.
Now, for each l we have to count the number of subsequences of s with length l that do not have equal adjacent characters. There are several ways to do that. One way is storing dpc, l — the number of valid subsequences of length l that end in character c. We now process characters one by one and recompute values of dpc, l. If the new character is ci, then clearly values of dpc, l with c ≠ ci do not change. We can also see that we can find the new values of dpci, l with the formula