CF1393E2.Twilight and Ancient Scroll (harder version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 109+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=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 .
输入格式
The first line contains a single integer n ( 1≤n≤105 ): 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 106 .
输出格式
Print one integer: the number of ways to get a version of the original from the scroll modulo 109+7 .
输入输出样例
输入#1
3 abcd zaza ataka
输出#1
4
输入#2
4 dfs bfs sms mms
输出#2
8
输入#3
3 abc bcd a
输出#3
0
输入#4
6 lapochka kartyshka bigbabytape morgenshtern ssshhhiiittt queen
输出#4
2028
说明/提示
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).