Twilight and Ancient Scroll (harder version)
Contest: Codeforces Round 662 (Div. 2)
Difficulty rating: 3200
Tags: dp, hashing, implementation, string suffix structures, strings, two pointers

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 format:
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 format:
Print one integer: the number of ways to get a version of the original from the scroll modulo $$$10^9+7$$$.

Constraints:
Time limit: 1.5 s
Memory limit: 256.0 MB

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).

Example input:
3
abcd
zaza
ataka

Example output:
4

Example input:
4
dfs
bfs
sms
mms

Example output:
8

Editorial:
Let's use dynamic programming. $$$dp[i][j]$$$: the number of ways to form the non-decreasing subsequence on the strings $$$1 \ldots i$$$, s.t. the delete character in string $$$i$$$ is $$$j$$$. This works in $$$O(L^3)$$$, where $$$L$$$ is the total length of all strings.
Let's optimize this solution. For each string, sort all strings obtained by deleting at most one character from this string. You can do it in $$$O(L^2 \cdot log L)$$$ for all strings. Then you can use two pointers to calculate or dp. To calculate the answer for the layer $$$i$$$ we will consider strings in the sorted order, and add all dp values for the smaller strings. We can calculate this dp in $$$O(L^2)$$$ and solve the problem in $$$O(L^2 \cdot log L)$$$. We can use binary search and hash to compare strings in $$$O(log L)$$$. Then you can sort all the strings in $$$O(L \cdot log^2 L)$$$.
Note that you can sort the strings in $$$O(L)$$$. Look at the string $$$s$$$. For each character find the first character to the right not equal to it (array $$$nxt[i]$$$). Then we will store two pointers: to the beginning and the end of the list. Consider characters in the order from left to right. If $$$s_i$$$ > $$$s_{nxt[i]}$$$, add $$$i$$$ to the beginning of the list (to the position $$$l$$$ and increase $$$l$$$ by $$$1$$$), otherwise add it to the end of the list (to the position $$$r$$$ and decrease $$$r$$$ by $$$1$$$). Then add $$$s$$$ to some position in the list ($$$s$$$ will be in the list after it without the last character). Then you can use the hash to make the comparisons for two pointers in $$$O(log L)$$$. This sol works in $$$O(L \cdot log L)$$$ and fits into TL.