Problem: 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 ≠ 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 ≤ n ≤ 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.

Examples

Input


3
abcd
zaza
ataka


Output


4


Input


4
dfs
bfs
sms
mms


Output


8


Input


3
abc
bcd
a


Output


0


Input


6
lapochka
kartyshka
bigbabytape
morgenshtern
ssshhhiiittt
queen


Output


2028

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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int L = 1000005;
const int mo = 1000000007;
int n, l[N];
vector<int> f[N];
string s[N];
char s1[L], s2[L];
int f1[L], f2[L], lcp[L];
void UPD(int &x, int y) { (x += y) >= mo ? x -= mo : 233; }
void DIAO(int x) {
  int mx = max(l[x], l[x - 1]) + 2;
  for (int i = (int)(0); i <= (int)(mx + 2); i++) {
    s1[i] = s2[i] = 'a' - 1;
    f1[i] = f2[i] = lcp[i] = 0;
  }
  for (int i = (int)(0); i <= (int)(l[x - 1]); i++) f1[i] = f[x - 1][i];
  for (int i = (int)(0); i <= (int)(l[x - 1] - 1); i++) s1[i] = s[x - 1][i];
  for (int i = (int)(0); i <= (int)(l[x] - 1); i++) s2[i] = s[x][i];
  for (int i = (int)(mx + 1); i >= (int)(0); i--)
    lcp[i] = (s1[i] == s2[i] ? lcp[i + 1] + 1 : 0);
  int v1 = 0, v2 = 0, fl = 0;
  for (int i = (int)(0); i <= (int)(mx); i++) {
    if (i) {
      if (s1[i] < s2[i - 1]) UPD(v2, v1), v1 = 0;
      if (s1[i] > s2[i - 1]) v1 = 0;
    }
    if (fl == 0) UPD(v1, f1[i]);
    if (fl == 1) UPD(v2, f1[i]);
    if (!fl && s1[i] != s2[i]) fl = (s1[i] < s2[i] ? 1 : -1);
    f2[i] = v2;
    int p = i + 1 + lcp[i + 1];
    if (p > mx || s1[p] < s2[p]) UPD(f2[i], v1);
  }
  v1 = v2 = 0;
  for (int i = (int)(mx); i >= (int)(0); i--) {
    int p = lcp[0];
    if (p >= i)
      UPD(f2[i], v1);
    else if (s1[p] < s2[p]) {
      UPD(f2[i], v1);
      UPD(f2[i], v2);
    }
    p = i + 1 + lcp[i + 1];
    if (p > mx || s1[p] < s2[p])
      UPD(v1, f1[i]);
    else
      UPD(v2, f1[i]);
    if (s1[i - 1] < s2[i]) UPD(v1, v2), v2 = 0;
    if (s1[i - 1] > s2[i]) UPD(v2, v1), v1 = 0;
  }
  for (int i = (int)(0); i <= (int)(l[x]); i++) {
    f[x][i] = f2[i];
  }
}
int main() {
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n); i++) cin >> s[i];
  for (int i = (int)(1); i <= (int)(n); i++) l[i] = s[i].length();
  for (int i = (int)(1); i <= (int)(n); i++) f[i].resize(l[i] + 1);
  for (int i = (int)(0); i <= (int)(l[1]); i++) f[1][i] = 1;
  for (int i = (int)(2); i <= (int)(n); i++) DIAO(i);
  int ans = 0;
  for (int i = (int)(0); i <= (int)(l[n]); i++) UPD(ans, f[n][i]);
  cout << ans << endl;
}