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;
int pw[1000005];
void calc(char *s, int *u, int n) {
  int cur = 1, L = 1, R = n;
  for (int i = 1; i <= n; i++)
    if (i == n) {
      for (int j = n; j >= R + 1; j--) u[j + 1] = u[j];
      while (cur) cur--, u[L++] = i - cur;
      u[L] = n + 1;
    } else if (s[i] == s[i + 1])
      cur++;
    else {
      if (s[i] < s[i + 1]) {
        while (cur) cur--, u[R--] = i - cur;
      } else {
        while (cur) cur--, u[L++] = i - cur;
      }
      cur = 1;
    }
}
void init(char *s, int *u, int n) {
  u[0] = 0;
  for (int i = 1; i <= n; i++) u[i] = (1LL * u[i - 1] * 233 + s[i]) % 777777773;
}
int get(int *u, int x, int ban) {
  if (x < ban) return u[x];
  int res = x - ban + 1;
  return (1LL * u[ban - 1] * pw[res] % 777777773 + u[x + 1] -
          1LL * u[ban] * pw[res] % 777777773 + 777777773) %
         777777773;
}
bool cmp(char *s, char *t, int *u, int *v, int x, int y, int lenu, int lenv) {
  int ed = ~0, l = 1, r = min(lenu - (x <= lenu), lenv - (y <= lenv));
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (get(u, mid, x) == get(v, mid, y))
      l = mid + 1;
    else
      r = mid - 1, ed = mid;
  }
  if (~ed)
    return s[ed + (ed >= x)] < t[ed + (ed >= y)];
  else
    return lenu - (x <= lenu) <= lenv - (y <= lenv);
}
int n, f[1000005], u[1000005], v[1000005], g[1000005], hs1[1000005],
    hs2[1000005];
char s[1000005], t[1000005];
int main() {
  pw[0] = 1;
  for (int i = 1; i <= 1000005 - 5; i++)
    pw[i] = 1LL * pw[i - 1] * 233 % 777777773;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int w = strlen(s + 1);
  calc(s, u, w);
  init(s, hs1, w);
  for (int i = 1; i <= w + 1; i++) f[i] = 1;
  for (int op = 2; op <= n; op++) {
    scanf("%s", t + 1);
    int m = strlen(t + 1), j = 1, sum = 0;
    calc(t, v, m);
    init(t, hs2, m);
    for (int i = 1; i <= m + 1; i++) g[i] = 0;
    for (int i = 1; i <= m + 1; i++) {
      while (j <= w + 1 && cmp(s, t, hs1, hs2, u[j], v[i], w, m))
        sum = (sum + f[j++]) % 1000000007;
      g[i] = sum;
    }
    w = m;
    for (int i = 1; i <= m + 1; i++)
      f[i] = g[i], s[i] = t[i], u[i] = v[i], hs1[i] = hs2[i];
  }
  int ans = 0;
  for (int i = 1; i <= w + 1; i++) ans = (ans + f[i]) % 1000000007;
  printf("%d\n", ans);
  return 0;
}