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;
std::mt19937 rnd(
    (int)std::chrono::steady_clock::now().time_since_epoch().count());
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
const int MAXSUMLEN = 1000000;
const int MOD = 1000000007;
void inc(int& a, int b) {
  if ((a += b) >= MOD) a -= MOD;
}
void dec(int& a, int b) { inc(a, MOD - b); }
int n;
char s[MAXSUMLEN + 1];
int offset[MAXN + 1], len[MAXN];
int dpnorem[MAXN];
int dprem[MAXSUMLEN];
char a[MAXSUMLEN + 1];
int alen;
char b[MAXSUMLEN + 1];
int blen;
int dpa[MAXSUMLEN];
int dpb[MAXSUMLEN];
int prefixcmp[MAXSUMLEN + 1];
int suffixcmp[MAXSUMLEN + 1][3];
int prefsubdp[MAXSUMLEN + 1][3];
int suffsubdp[MAXSUMLEN + 1][3];
int combinecmp(int a, int b) { return a != 0 ? a : b; }
int cmpchar(int aidx, int bidx) {
  char ca = 0 <= aidx && aidx < alen ? a[aidx] : '\0',
       cb = 0 <= bidx && bidx < blen ? b[bidx] : '\0';
  return ca < cb ? -1 : ca > cb ? +1 : 0;
}
int solve() {
  dpnorem[n - 1] = 1;
  for (int j = (0); j < (len[n - 1]); ++j) dprem[offset[n - 1] + j] = 1;
  for (int i = n - 2; i >= 0; --i) {
    alen = len[i];
    for (int j = (0); j < (alen); ++j) a[j] = s[offset[i] + j];
    a[alen] = '\0';
    blen = len[i + 1];
    for (int k = (0); k < (blen); ++k) b[k] = s[offset[i + 1] + k];
    b[blen] = '\0';
    for (int k = (0); k < (blen); ++k) dpb[k] = dprem[offset[i + 1] + k];
    int mxlen = max(alen, blen);
    prefixcmp[0] = 0;
    for (int j = (0); j < (mxlen); ++j)
      prefixcmp[j + 1] = combinecmp(prefixcmp[j], cmpchar(j, j));
    for (int shift = (-1); shift <= (+1); ++shift) {
      suffixcmp[mxlen][shift + 1] = 0;
      for (int j = mxlen - 1; j >= 0; --j)
        suffixcmp[j][shift + 1] =
            combinecmp(cmpchar(j, j + shift), suffixcmp[j + 1][shift + 1]);
    }
    for (int j = (0); j < (mxlen); ++j) {
      for (int cmp = (-1); cmp <= (+1); ++cmp) prefsubdp[j][cmp + 1] = 0;
      if (j - 1 >= 0)
        for (int prefcmp = (-1); prefcmp <= (+1); ++prefcmp) {
          int cmp = combinecmp(prefcmp, cmpchar(j - 1, j));
          inc(prefsubdp[j][cmp + 1], prefsubdp[j - 1][prefcmp + 1]);
        }
      if (j < blen) {
        int cmp = prefixcmp[j];
        inc(prefsubdp[j][cmp + 1], dpb[j]);
      }
    }
    for (int j = mxlen - 1; j >= 0; --j) {
      for (int cmp = (-1); cmp <= (+1); ++cmp) suffsubdp[j][cmp + 1] = 0;
      if (j + 1 < mxlen)
        for (int suffcmp = (-1); suffcmp <= (+1); ++suffcmp) {
          int cmp = combinecmp(cmpchar(j + 1, j), suffcmp);
          inc(suffsubdp[j][cmp + 1], suffsubdp[j + 1][suffcmp + 1]);
        }
      if (j < blen) {
        int cmp = suffixcmp[j + 1][0 + 1];
        inc(suffsubdp[j][cmp + 1], dpb[j]);
      }
    }
    dpnorem[i] = 0;
    if (suffixcmp[0][0 + 1] <= 0) inc(dpnorem[i], dpnorem[i + 1]);
    for (int k = (0); k < (blen); ++k) {
      int cmp = combinecmp(prefixcmp[k], suffixcmp[k][+1 + 1]);
      if (cmp <= 0) inc(dpnorem[i], dpb[k]);
    }
    for (int j = (0); j < (alen); ++j) {
      dpa[j] = 0;
      for (int prefcmp = (-1); prefcmp <= (+1); ++prefcmp) {
        int cmp = combinecmp(prefcmp, suffixcmp[j + 1][0 + 1]);
        if (cmp <= 0) inc(dpa[j], prefsubdp[j][prefcmp + 1]);
      }
      for (int suffcmp = (-1); suffcmp <= (+1); ++suffcmp) {
        int cmp = combinecmp(prefixcmp[j], suffcmp);
        if (cmp <= 0) inc(dpa[j], suffsubdp[j][suffcmp + 1]);
      }
      if (j < blen) {
        int cmp = combinecmp(prefixcmp[j], suffixcmp[j + 1][0 + 1]);
        if (cmp <= 0) dec(dpa[j], dpb[j]);
      }
      {
        int cmp = combinecmp(prefixcmp[j], suffixcmp[j + 1][-1 + 1]);
        if (cmp <= 0) inc(dpa[j], dpnorem[i + 1]);
      }
    }
    for (int j = (0); j < (alen); ++j) dprem[offset[i] + j] = dpa[j];
  }
  int ret = 0;
  inc(ret, dpnorem[0]);
  for (int j = (0); j < (len[0]); ++j) inc(ret, dprem[offset[0] + j]);
  return ret;
}
void run() {
  scanf("%d", &n);
  offset[0] = 0;
  for (int i = (0); i < (n); ++i) {
    scanf("%s", s + offset[i]);
    len[i] = strlen(s + offset[i]);
    offset[i + 1] = offset[i] + len[i];
  }
  printf("%d\n", solve());
}
int main() {
  run();
  return 0;
}