#P6273. [eJOI 2017] 魔法

[eJOI 2017] 魔法

Problem Description

Given a string SS of length nn. Let the number of distinct characters in SS be kk.

A substring of a string is any consecutive segment of that string.

A magic substring is defined as a non-empty substring of SS that satisfies: the number of distinct characters in this substring is kk, and the number of occurrences of each character is the same.

You need to find the number of different magic substrings of the given string SS.

If two substrings have different left or right endpoints, then they are considered different.

Input Format

The first line: an integer nn, the length of the string.

The second line: a string SS.

Output Format

Output one integer, the value of the answer mod(109+7)\bmod (10^9+7).

8
abccbabc
4
7
abcABCC
1
20
SwSSSwwwwSwSwwSwwwwS
22

Hint

Sample Explanation

Sample 1 Explanation

  • The substrings that satisfy the condition are: $\texttt{abc},\texttt{cba},\texttt{abc},\texttt{abccba}$.

Sample 2 Explanation

  • Only the substring abcABC\texttt{abcABC} is a magic substring (case-sensitive, i.e. aA\texttt{a}\ne \texttt{A}).

Sample 3 Explanation

  • One of them is SwSwwS\texttt{SwSwwS}.

Constraints

This problem uses bundled testdata, with 4 subtasks in total.

  • Subtask 1 (10 points): 2n1002\le n\le 100.
  • Subtask 2 (20 points): 2n2×1032\le n\le 2\times 10^3.
  • Subtask 3 (30 points): 2n105,k=22\le n\le 10^5,k=2 (i.e. there are only two kinds of characters in SS).
  • Subtask 4 (40 points): no other limits.

For all testdata, it is guaranteed that 2n1052\le n\le 10^5, and the character set is $[\texttt{a},\texttt{z}] \cup [\texttt{A},\texttt{Z}]$.

Notes

The original problem is from: eJOI 2017 Problem A Magic.

Translation provided by: @_Wallace_.

Translated by ChatGPT 5