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 ANSMOD = (int)1e9 + 7;
const int P = 37, MOD = (int)1e9 + 7;
const int N = (int)1e6 + 10;
int f[2][N], pos[2][N], pw[N], n, len[N];
char s[N];
vector<int> a[N], ha[N];
inline void add(int& x, int y) {
  x += y;
  if (x >= ANSMOD) x -= ANSMOD;
}
int hsh(int id, int len, int x) {
  if (!x || len < x)
    return ha[id][len];
  else
    return (1ll * (ha[id][x - 1] - ha[id][x] + MOD) * pw[len - x + 1] +
            ha[id][len + 1]) %
           MOD;
}
bool ok(int s1, int x, int s2, int y) {
  int l = 0, r = min(len[s1] + (!x), len[s2] + (!y));
  for (; l < r - 1;) {
    int mid = (l + r) >> 1;
    if (hsh(s1, mid, x) != hsh(s2, mid, y))
      r = mid;
    else
      l = mid;
  }
  return l + 1 + (!!x) > len[s1] ||
         (l + 1 + (!!y) <= len[s2] &&
          a[s1][!x || l + 1 < x ? l + 1 : l + 2] <=
              a[s2][!y || l + 1 < y ? l + 1 : l + 2]);
}
int main() {
  scanf("%d", &n);
  pw[0] = 1;
  for (int i = 1; i <= 1000000; i++) pw[i] = 1ll * pw[i - 1] * P % MOD;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    len[i] = strlen(s);
    a[i].resize(len[i] + 1);
    ha[i].resize(len[i] + 1);
    for (int j = 1; j <= len[i]; j++) a[i][j] = s[j - 1] - 'a' + 1;
    for (int j = 1; j <= len[i]; j++)
      ha[i][j] = (1ll * ha[i][j - 1] * P + a[i][j]) % MOD;
  }
  for (int i = 1; i <= n; i++) {
    int l = 0, r = len[i] + 2;
    for (int j = 1; j <= len[i]; j++) {
      int k = j;
      char c = a[i][j];
      for (; k <= len[i] && a[i][k] == c; k++)
        ;
      if (k > len[i] || a[i][k] < c)
        for (int p = j; p < k; p++) pos[i & 1][++l] = p;
      else
        for (int p = j; p < k; p++) pos[i & 1][--r] = p;
      j = k - 1;
    }
    pos[i & 1][++l] = 0;
    if (i == 1)
      for (int j = 1; j <= len[i] + 1; j++) f[i & 1][j - 1] = 1;
    else {
      int tmp = 0, sum = 0;
      for (int j = 1; j <= len[i] + 1; j++) {
        for (; tmp < len[i - 1] + 1 &&
               ok(i - 1, pos[(i & 1) ^ 1][tmp + 1], i, pos[i & 1][j]);) {
          tmp++;
          add(sum, f[(i & 1) ^ 1][pos[(i & 1) ^ 1][tmp]]);
        }
        f[i & 1][pos[i & 1][j]] = sum;
      }
    }
  }
  int ans = 0;
  for (int j = 1; j <= len[n] + 1; j++) add(ans, f[n & 1][j - 1]);
  printf("%d\n", ans);
  return 0;
}