This page is still under construction.

Parts of this page are still being built. What you see may change.

Distinct Substring Queries

Time limit2sMemory limit512 MB

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

Hard9 of 10

Topics
String, String matching, Sorting, Trie
Solved
No attempts yet

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 (1≤Q≤1 000 0001 \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 1 000 000 0071\,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 27 mod 1 000 000 007=2727 \bmod 1\,000\,000\,007 = 27, so the answer is 27.

Examples1

  1. Example 1

    Input
    8
    + a
    + b
    + a
    + a
    -
    -
    -
    + a
    
    Expected output
    27