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 md = 1e9 + 7;
int sa[1100000];
int sn[1100000];
int Rank[1100000], height[1100000];
int wa[1100000], wb[1100000], wv[1100000], wt[1100000];
int cmp(int *r, int a, int b, int l) {
  return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *r, int *sa, int n, int m) {
  int i, j, p, *x = wa, *y = wb, *t;
  for (i = 0; i < m; i++) wt[i] = 0;
  for (i = 0; i < n; i++) wt[x[i] = r[i]]++;
  for (i = 1; i < m; i++) wt[i] += wt[i - 1];
  for (i = n - 1; i >= 0; i--) sa[--wt[x[i]]] = i;
  for (j = 1, p = 1; p < n; j *= 2, m = p) {
    for (p = 0, i = n - j; i < n; i++) y[p++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= j) y[p++] = sa[i] - j;
    for (i = 0; i < n; i++) wv[i] = x[y[i]];
    for (i = 0; i < m; i++) wt[i] = 0;
    for (i = 0; i < n; i++) wt[wv[i]]++;
    for (i = 1; i < m; i++) wt[i] += wt[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--wt[wv[i]]] = y[i];
    for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
      x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
  }
  return;
}
void calheight(int *r, int *sa, int n) {
  int i, j, k = 0;
  for (i = 1; i <= n; i++) Rank[sa[i]] = i;
  for (i = 0; i < n; height[Rank[i++]] = k)
    for (k ? k-- : 0, j = sa[Rank[i] - 1]; r[i + k] == r[j + k]; k++)
      ;
  return;
}
int Rn[22][1100000];
void rmqInit(int n) {
  int i, j;
  for (i = 0; i < 22; i++) memset(Rn[i], 0, n * 4);
  for (i = 1; i < n; i++) Rn[0][i] = height[i];
  for (i = 1; (1 << i) < n; i++)
    for (j = 1; j + (1 << i) - 1 < n; j++)
      Rn[i][j] = min(Rn[i - 1][j], Rn[i - 1][j + (1 << (i - 1))]);
}
int rmq(int a, int b) {
  int len = b - a + 1;
  int i = 0;
  while ((1 << (i + 1)) <= len) i++;
  return min(Rn[i][a], Rn[i][b - (1 << i) + 1]);
}
int lcp(int a, int b) {
  int ra = Rank[a], rb = Rank[b];
  if (ra > rb) swap(ra, rb);
  return rmq(ra + 1, rb);
}
char cn[2][1100000];
bool bn[1100000];
int st[2][1100000];
int dp[2][1100000];
int len[2];
void input(int a) {
  scanf("%s", cn[a]);
  int l = strlen(cn[a]);
  cn[a][l] = 'A', cn[a][++l] = '\0';
  int i;
  for (i = l - 1; i >= 0; i--) {
    if (cn[a][i] > cn[a][i + 1])
      bn[i] = 1;
    else if (cn[a][i] < cn[a][i + 1])
      bn[i] = 0;
    else
      bn[i] = bn[i + 1];
  }
  int k = 0;
  for (i = 0; i < l; i++)
    if (bn[i]) st[a][k++] = i;
  for (i = l - 1; i >= 0; i--)
    if (!bn[i]) st[a][k++] = i;
  len[a] = l;
}
char s1[1100000], s2[1100000];
bool stdcmp(int j, int k, int a) {
  int i2 = st[a][j], i1 = st[a ^ 1][k];
  int len2 = len[a], len1 = len[a ^ 1];
  int i, t;
  for (i = t = 0; i <= len1; i++) {
    if (i == i1) continue;
    s1[t++] = cn[a ^ 1][i];
  }
  for (i = t = 0; i <= len2; i++) {
    if (i == i2) continue;
    s2[t++] = cn[a][i];
  }
  if (strcmp(s2, s1) >= 0)
    return 1;
  else
    return 0;
}
int cmp(int l2, int r2, int l1, int r1, int n) {
  if (r2 - l2 < 0 && r1 - l1 < 0) return 0;
  if (r2 - l2 < 0) return -1;
  if (r1 - l1 < 0) return 1;
  int len2 = r2 - l2 + 1, len1 = r1 - l1 + 1;
  int mlen = min(len1, len2);
  int a2 = n + 1 + l2, a1 = l1;
  int tlen = lcp(a2, a1);
  if (tlen < mlen)
    return Rank[a2] - Rank[a1];
  else
    return len2 - len1;
}
bool greaterOrEqual(int j, int k, int a) {
  int i2 = st[a][j], i1 = st[a ^ 1][k];
  int len2 = len[a], len1 = len[a ^ 1];
  int l1[3], r1[3], l2[3], r2[3];
  if (i2 < i1) {
    l1[0] = 0, r1[0] = i2 - 1;
    l2[0] = 0, r2[0] = i2 - 1;
    l1[1] = i2, r1[1] = i1 - 1;
    l2[1] = i2 + 1, r2[1] = i1;
    l1[2] = i1 + 1, r1[2] = len1 - 1;
    l2[2] = i1 + 1, r2[2] = len2 - 1;
  } else if (i2 > i1) {
    l1[0] = 0, r1[0] = i1 - 1;
    l2[0] = 0, r2[0] = i1 - 1;
    l1[1] = i1 + 1, r1[1] = i2;
    l2[1] = i1, r2[1] = i2 - 1;
    l1[2] = i2 + 1, r1[2] = len1 - 1;
    l2[2] = i2 + 1, r2[2] = len2 - 1;
  } else {
    l1[0] = 0, r1[0] = i2 - 1;
    l2[0] = 0, r2[0] = i2 - 1;
    l1[1] = 0, r1[1] = -1;
    l2[1] = 0, r2[1] = -1;
    l1[2] = i2 + 1, r1[2] = len1 - 1;
    l2[2] = i2 + 1, r2[2] = len2 - 1;
  }
  int i;
  for (i = 0; i < 3; i++)
    if (r1[i] > len1 - 2) r1[i] = len1 - 2;
  for (i = 0; i < 3; i++)
    if (r2[i] > len2 - 2) r2[i] = len2 - 2;
  for (i = 0; i < 3; i++) {
    int ret = cmp(l2[i], r2[i], l1[i], r1[i], len1);
    if (ret > 0) return 1;
    if (ret < 0) return 0;
  }
  return 1;
}
int main() {
  int n;
  int i, j, k;
  cin >> n;
  input(0);
  for (i = 0; i < len[0]; i++) dp[0][i] = 1;
  for (i = 1; i < n; i++) {
    int a = i & 1, b = i - 1 & 1;
    input(a);
    int lenab = len[a] + len[b] + 1;
    for (j = 0; j < len[b]; j++) sn[j] = cn[b][j];
    sn[j++] = '@';
    for (; j < lenab; j++) sn[j] = cn[a][j - len[b] - 1];
    sn[j] = '\0';
    da(sn, sa, lenab + 1, 128);
    calheight(sn, sa, lenab);
    rmqInit(lenab + 1);
    int k = 0;
    int sum = 0;
    bool bl = 0;
    for (j = 0; j < len[a]; j++) {
      while (k < len[b] && greaterOrEqual(j, k, a)) {
        sum = (sum + dp[b][k]) % md;
        k++;
      }
      dp[a][j] = sum;
      if (dp[a][j] != 0) bl = 1;
    }
  }
  int a = n - 1 & 1;
  int ans = 0;
  for (i = 0; i < len[a]; i++) ans = (ans + dp[a][i]) % md;
  cout << ans << endl;
  return 0;
}