Codeforces Round 662 (Div. 2)
E2
Twilight and Ancient Scroll (harder version)
Codeforces
3200
原题
← E1→
翻译中…

This is a harder version of the problem E with larger constraints.

翻译中…

Twilight Sparkle has received a new task from Princess Celestia. This time she asked to decipher the ancient scroll containing important knowledge of pony origin.

翻译中…

To hide the crucial information from evil eyes, pony elders cast a spell on the scroll. That spell adds exactly one letter in any place to each word it is cast on. To make the path to the knowledge more tangled elders chose some of words in the scroll and cast a spell on them.

翻译中…

Twilight Sparkle knows that the elders admired the order in all things so the scroll original scroll contained words in lexicographically non-decreasing order. She is asked to delete one letter from some of the words of the scroll (to undo the spell) to get some version of the original scroll.

翻译中…

Unfortunately, there may be more than one way to recover the ancient scroll. To not let the important knowledge slip by Twilight has to look through all variants of the original scroll and find the required one. To estimate the maximum time Twilight may spend on the work she needs to know the number of variants she has to look through. She asks you to find that number! Since that number can be very big, Twilight asks you to find it modulo $10^9+7$.

翻译中…

It may occur that princess Celestia has sent a wrong scroll so the answer may not exist.

翻译中…

A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds:

翻译中…
  • $a$ is a prefix of $b$, but $a \ne b$;
  • in the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$.
翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($1 \le n \le 10^5$): the number of words in the scroll.

翻译中…

The $i$-th of the next $n$ lines contains a string consisting of lowercase English letters: the $i$-th word in the scroll. The length of each word is at least one. The sum of lengths of words does not exceed $10^6$.

翻译中…

Output

翻译中…

Print one integer: the number of ways to get a version of the original from the scroll modulo $10^9+7$.

翻译中…

Note

翻译中…

Notice that the elders could have written an empty word (but they surely cast a spell on it so it holds a length $1$ now).

翻译中…

样例

Input #1
3
abcd
zaza
ataka
Output #1
4
InputCopy OutputCopy
Input #2
4
dfs
bfs
sms
mms
Output #2
8
InputCopy OutputCopy
Input #3
3
abc
bcd
a
Output #3
0
InputCopy OutputCopy
Input #4
6
lapochka
kartyshka
bigbabytape
morgenshtern
ssshhhiiittt
queen
Output #4
2028
InputCopy OutputCopy
题目信息
题号
1393E2
时间限制
1.5 s
内存限制
256 MB
标签
动态规划
哈希
模拟
后缀结构
字符串
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。