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 = 1000005;
const int M = 1000000007;
int n, i, lp[N], j, a[N], la[N], ln, lcp[3][N], t;
char c[N], lc[N];
int dp[N], ldp[N];
bool cmp(int u, int v) {
  if (lp[min(u, v)] >= abs(v - u)) return u < v;
  int fl = 0;
  if (u > v) {
    swap(u, v);
    fl = 1;
  }
  return (c[u + lp[u] + 1] < c[u + lp[u]]) ^ fl;
}
int main() {
  scanf("%d", &t);
  for (int m = 1; m <= t; ++m) {
    for (i = 1; i <= n; ++i) c[i] = 0;
    scanf("%s", c + 1);
    n = strlen(c + 1);
    for (i = 1; i <= n;) {
      for (j = i; c[i] == c[j]; ++j)
        ;
      int p = j;
      for (j = i; c[i] == c[j]; ++j) lp[j] = p - j - 1;
      i = j;
    }
    for (i = 1; i <= n; ++i) a[i] = i;
    sort(a + 1, a + 1 + n, cmp);
    for (i = 1; i <= n; ++i)
      if (a[i] + lp[a[i]] + 1 <= n &&
          c[a[i] + lp[a[i]] + 1] > c[a[i] + lp[a[i]]]) {
        for (j = n + 1; j > i; --j) a[j] = a[j - 1];
        break;
      }
    a[i] = 0;
    if (m == 1) {
      for (i = 1; i <= n + 1; ++i) dp[i] = i;
    } else {
      for (int f = -1; f <= 1; ++f)
        for (i = 1; i <= ln; ++i) {
          lcp[f + 1][i] = max(lcp[f + 1][i - 1] - 1, 0);
          while (i + lcp[f + 1][i] <= ln && i + f + lcp[f + 1][i] <= n &&
                 lc[i + lcp[f + 1][i]] == c[i + f + lcp[f + 1][i]])
            ++lcp[f + 1][i];
        }
      int l = 1;
      for (i = 1; i <= n + 1; ++i) {
        while (l <= ln + 1) {
          if (la[l] != 0 && a[i] != 0) {
            if (la[l] < a[i]) {
              if (lcp[1][1] < la[l] - 1) {
                if (lc[1 + lcp[1][1]] > c[1 + lcp[1][1]]) break;
              } else if (lcp[0][la[l] + 1] < a[i] - la[l]) {
                if (lc[la[l] + 1 + lcp[0][la[l] + 1]] >
                    c[la[l] + lcp[0][la[l] + 1]])
                  break;
              } else if (lc[a[i] + 1 + lcp[1][a[i] + 1]] >
                         c[a[i] + 1 + lcp[1][a[i] + 1]])
                break;
            }
            if (la[l] == a[i]) {
              if (lcp[1][1] < la[l] - 1) {
                if (lc[1 + lcp[1][1]] > c[1 + lcp[1][1]]) break;
              } else {
                if (lc[a[i] + 1 + lcp[1][a[i] + 1]] >
                    c[a[i] + 1 + lcp[1][a[i] + 1]])
                  break;
              }
            }
            if (la[l] > a[i]) {
              if (lcp[1][1] < a[i] - 1) {
                if (lc[1 + lcp[1][1]] > c[1 + lcp[1][1]]) break;
              } else if (lcp[2][a[i]] < la[l] - a[i]) {
                if (lc[a[i] + lcp[2][a[i]]] > c[a[i] + 1 + lcp[2][a[i]]]) break;
              } else if (lc[la[l] + 1 + lcp[1][la[l] + 1]] >
                         c[la[l] + 1 + lcp[1][la[l] + 1]])
                break;
            }
          }
          if (la[l] != 0 && a[i] == 0) {
            if (lcp[1][1] < la[l] - 1) {
              if (lc[1 + lcp[1][1]] > c[1 + lcp[1][1]]) break;
            } else if (lc[la[l] + 1 + lcp[0][la[l] + 1]] >
                       c[la[l] + lcp[0][la[l] + 1]])
              break;
          }
          if (la[l] == 0 && a[i] != 0) {
            if (lcp[1][1] < a[i] - 1) {
              if (lc[1 + lcp[1][1]] > c[1 + lcp[1][1]]) break;
            } else if (lc[a[i] + lcp[2][a[i]]] > c[a[i] + 1 + lcp[2][a[i]]])
              break;
          }
          if (la[l] == 0 && a[i] == 0)
            if (lc[1 + lcp[1][1]] > c[1 + lcp[1][1]]) break;
          ++l;
        }
        dp[i] = ldp[l - 1];
      }
      for (i = 1; i <= n + 1; ++i) dp[i] = (dp[i - 1] + dp[i]) % M;
    }
    for (i = 1; i <= ln; ++i) lc[i] = 0;
    for (i = 1; i <= n; ++i) lc[i] = c[i];
    for (i = 1; i <= n + 1; ++i) {
      la[i] = a[i];
      ldp[i] = dp[i];
    }
    ln = n;
  }
  cout << dp[n + 1] << endl;
}