Distinct Substring Queries

Maintain a string under push-back and pop-front operations, reporting the number of distinct substrings after each of up to a million queries.

Hard9StringString matchingSortingTrieNo attempts yetTime limit2sMemory limit512 MB

Problem

The string SS is empty at the start. Write a program that performs the following two kinds of queries in order.

  • + c: append the character cc to the end of SS. cc is a lowercase English letter.
  • -: remove the first character of SS.

Right after each query, the length of SS is always positive.

After each query, you must find the number of distinct substrings of SS at that moment.

Input

The first line contains the number of queries QQ (1Q10000001 \le Q \le 1\,000\,000).

Each of the next QQ lines contains one query.

Output

For each query, find the number of distinct substrings of SS right after that query. Print the sum of these numbers over all queries, modulo 10000000071\,000\,000\,007.

Note

In the first example, the state after each query is as follows.

  1. SS = a. It has 1 distinct substring: a.
  2. SS = ab. It has 3 distinct substrings: a, b, ab.
  3. SS = aba. It has 5 distinct substrings: a, b, ab, ba, aba.
  4. SS = abaa. It has 8 distinct substrings: a, b, ab, ba, aa, aba, baa, abaa.
  5. SS = baa. It has 5 distinct substrings: a, b, ba, aa, baa.
  6. SS = aa. It has 2 distinct substrings: a, aa.
  7. SS = a. It has 1 distinct substring: a.
  8. SS = aa. It has 2 distinct substrings: a, aa.

The sum is 1+3+5+8+5+2+1+2=271 + 3 + 5 + 8 + 5 + 2 + 1 + 2 = 27, and 27mod1000000007=2727 \bmod 1\,000\,000\,007 = 27, so the answer is 27.