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 INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const long long M = 1e9 + 7;
const long long maxn = 1e6 + 107;
const double pi = acos(-1.0);
const double eps = 0.0000000001;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline void pr2(T x, int k = 64) {
  long long i;
  for (i = 0; i < k; i++) fprintf(stderr, "%d", (x >> i) & 1);
  putchar(' ');
}
template <typename T>
inline void add_(T &A, int B, long long MOD = M) {
  A += B;
  (A >= MOD) && (A -= MOD);
}
template <typename T>
inline void mul_(T &A, long long B, long long MOD = M) {
  A = (A * B) % MOD;
}
template <typename T>
inline void mod_(T &A, long long MOD = M) {
  A %= MOD;
  A += MOD;
  A %= MOD;
}
template <typename T>
inline void max_(T &A, T B) {
  (A < B) && (A = B);
}
template <typename T>
inline void min_(T &A, T B) {
  (A > B) && (A = B);
}
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
inline long long powMM(long long a, long long b, long long mod = M) {
  long long ret = 1;
  for (; b; b >>= 1ll, a = a * a % mod)
    if (b & 1) ret = ret * a % mod;
  return ret;
}
int startTime;
void startTimer() { startTime = clock(); }
void printTimer() {
  fprintf(stderr, "/--- Time: %ld milliseconds ---/\n", clock() - startTime);
}
char str[maxn], nxt[maxn];
int dpnow[maxn], dpnxt[maxn];
int f1[maxn], f2[maxn], f3[maxn], f4[maxn];
int l[maxn], l_pre[maxn], l_suf[maxn];
int get(int A[], int l, int r) {
  if (l > r) return 0;
  return (A[r] - A[l - 1] + M) % M;
}
int main() {
  int _, i, j;
  int n;
  scanf("%d", &n);
  int lpre, lnow;
  dpnow[1] = 1;
  lpre = lnow = 0;
  for (_ = 1; _ <= n; _++) {
    scanf("%s", nxt + 1);
    lnow = strlen(nxt + 1);
    nxt[lnow + 1] = 1;
    for (i = lnow + 1; i >= 1; i--) {
      if (i > lpre || nxt[i] != str[i + 1])
        l_suf[i] = i;
      else
        l_suf[i] = l_suf[i + 1];
      if (i > lpre || nxt[i] != str[i - 1])
        l_pre[i] = i;
      else
        l_pre[i] = l_pre[i + 1];
      if (i > lpre || nxt[i] != str[i])
        l[i] = i;
      else
        l[i] = l[i + 1];
    }
    auto cmp = [&](int i, int j) {
      if (i == lnow + 1)
        return l[1] < j ? str[l[1]] <= nxt[l[1]]
                        : str[l_suf[j] + 1] <= nxt[l_suf[j]];
      if (j == lnow + 1)
        return l[1] < i ? str[l[1]] <= nxt[l[1]]
                        : str[l_pre[i + 1] - 1] <= nxt[l_pre[i + 1]];
      if (l[1] < min(i, j)) return str[l[1]] <= nxt[l[1]];
      if (i <= j)
        return l_pre[i + 1] - 1 < j ? str[l_pre[i + 1] - 1] <= nxt[l_pre[i + 1]]
                                    : str[l[j + 1]] <= nxt[l[j + 1]];
      if (j < i)
        return l_suf[j] < i ? str[l_suf[j] + 1] <= nxt[l_suf[j]]
                            : str[l[i + 1]] <= nxt[l[i + 1]];
      return false;
    };
    for (i = 1; i <= lnow + 1; i++) {
      dpnxt[i] = 0;
      int low = 0;
      if (i != lnow + 1) low = lpre + 1;
      for (j = low; j <= lpre + 1; j++) {
        if (i != lnow + 1 && j != lpre + 1) continue;
        if (cmp(i, j)) add_(dpnxt[i], dpnow[j]);
      }
    }
    {
      int j;
      for (j = 0; j <= max(lnow + 1, lpre + 1); j++)
        f1[j] = f2[j] = f3[j] = f4[j] = 0;
      for (j = 1; j <= max(lnow + 1, lpre + 1); j++) {
        if (j == lpre + 1) continue;
        if (j <= lpre + 1) f1[j] = dpnow[j];
        if (j <= lpre + 1 && str[l[j + 1]] <= nxt[l[j + 1]]) f2[j] = dpnow[j];
        if (j <= lpre + 1 && str[l_suf[j] + 1] <= nxt[l_suf[j]] && j <= l[1]) {
          int l = max(j + 1, l_suf[j] + 1), r = lnow + 1;
          if (l <= r) {
            add_(f3[l], dpnow[j]);
            add_(f3[r + 1], M - dpnow[j]);
          }
        }
        if (j <= lpre + 1 && j <= l[1]) {
          int l = j + 1, r = min(lnow + 1, l_suf[j]);
          if (l <= r) {
            add_(f4[l], dpnow[j]);
            add_(f4[r + 1], M - dpnow[j]);
          }
        }
      }
      for (j = 1; j <= max(lnow + 1, lpre + 1); j++) {
        add_(f1[j], f1[j - 1]);
        add_(f2[j], f2[j - 1]);
        add_(f3[j], f3[j - 1]);
        add_(f4[j], f4[j - 1]);
      }
    }
    for (i = 1; i <= lnow + 1; i++) {
      if (i == lnow + 1) continue;
      if (l[1] < i && str[l[1]] <= nxt[l[1]])
        add_(dpnxt[i], get(f1, i, lpre + 1));
      if (str[l[1]] <= nxt[l[1]]) add_(dpnxt[i], get(f1, l[1] + 1, i - 1));
      if (i <= l[1] && str[l_pre[i + 1] - 1] <= nxt[l_pre[i + 1]])
        add_(dpnxt[i], get(f1, max(i, l_pre[i + 1]), lpre + 1));
      if (i <= l[1])
        add_(dpnxt[i], get(f2, i, min(lpre + 1, l_pre[i + 1] - 1)));
      add_(dpnxt[i], f3[i]);
      if (str[l[i + 1]] <= nxt[l[i + 1]]) add_(dpnxt[i], f4[i]);
    }
    for (i = 1; i <= lpre + 1; i++) dpnow[i] = str[i] = 0;
    for (i = 1; i <= lnow + 1; i++) dpnow[i] = dpnxt[i], str[i] = nxt[i];
    str[lnow + 1] = 0;
    lpre = lnow;
  }
  int ans = 0;
  for (i = 1; i <= lnow + 1; i++) add_(ans, dpnow[i]);
  printf("%d\n", ans);
}