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 MX = 1000005, LG = 20, mod = 1000000007;
int sa[MX], lcp[MX], f[MX], S[MX];
inline bool leq(int a1, int a2, int b1, int b2) {
  return a1 < b1 || a1 == b1 && a2 <= b2;
}
inline bool leq(int a1, int a2, int a3, int b1, int b2, int b3) {
  return a1 < b1 || a1 == b1 && leq(a2, a3, b2, b3);
}
int c[2 * MX];
void radixPass(int a[], int b[], int r[], int n, int K) {
  for (int i = 0; i < K + 1; i++) c[i] = 0;
  for (int i = 0; i < n; i++) c[r[a[i]]]++;
  for (int i = 0, sum = 0; i <= K; i++) {
    int t = c[i];
    c[i] = sum;
    sum += t;
  }
  for (int i = 0; i < n; i++) b[c[r[a[i]]]++] = a[i];
}
void obtSA(int s[], int sa[], int n, int K) {
  int n0 = (n + 2) / 3, n1 = (n + 1) / 3, n2 = n / 3, n02 = n0 + n2;
  int s12[n02 + 3];
  s12[n02] = s12[n02 + 1] = s12[n02 + 2] = 0;
  int sa12[n02 + 3];
  sa12[n02] = sa12[n02 + 1] = sa12[n02 + 2] = 0;
  int s0[n0];
  int sa0[n0];
  for (int i = 0, j = 0; i < n + (n0 - n1); i++)
    if (i % 3 != 0) s12[j++] = i;
  radixPass(s12, sa12, s + 2, n02, K);
  radixPass(sa12, s12, s + 1, n02, K);
  radixPass(s12, sa12, s, n02, K);
  int name = 0, c0 = -1, c1 = -1, c2 = -1;
  for (int i = 0; i < n02; i++) {
    if (s[sa12[i]] != c0 || s[sa12[i] + 1] != c1 || s[sa12[i] + 2] != c2) {
      name++;
      c0 = s[sa12[i]];
      c1 = s[sa12[i] + 1];
      c2 = s[sa12[i] + 2];
    }
    if (sa12[i] % 3 == 1) {
      s12[sa12[i] / 3] = name;
    } else {
      s12[sa12[i] / 3 + n0] = name;
    }
  }
  if (name < n02) {
    obtSA(s12, sa12, n02, name);
    for (int i = 0; i < n02; i++) s12[sa12[i]] = i + 1;
  } else {
    for (int i = 0; i < n02; i++) sa12[s12[i] - 1] = i;
  }
  for (int i = 0, j = 0; i < n02; i++)
    if (sa12[i] < n0) s0[j++] = 3 * sa12[i];
  radixPass(s0, sa0, s, n0, K);
  for (int p = 0, t = n0 - n1, k = 0; k < n; k++) {
    int i = (sa12[t] < n0 ? sa12[t] * 3 + 1 : (sa12[t] - n0) * 3 + 2);
    int j = sa0[p];
    if (sa12[t] < n0 ? leq(s[i], s12[sa12[t] + n0], s[j], s12[j / 3])
                     : leq(s[i], s[i + 1], s12[sa12[t] - n0 + 1], s[j],
                           s[j + 1], s12[j / 3 + n0])) {
      sa[k] = i;
      t++;
      if (t == n02)
        for (k++; p < n0; p++, k++) sa[k] = sa0[p];
    } else {
      sa[k] = j;
      p++;
      if (p == n0)
        for (k++; t < n02; t++, k++)
          sa[k] = (sa12[t] < n0 ? sa12[t] * 3 + 1 : (sa12[t] - n0) * 3 + 2);
    }
  }
}
int phi[MX], plcp[MX];
void obtLCP(string &s) {
  int n = s.size();
  phi[sa[0]] = -1;
  for (int i = 1; i < n; i++) phi[sa[i]] = sa[i - 1];
  for (int i = 0, l = 0; i < n; i++) {
    if (phi[i] == -1) {
      plcp[i] = 0;
      continue;
    }
    while (s[i + l] == s[phi[i] + l]) l++;
    plcp[i] = l;
    l = max(l - 1, 0);
  }
  for (int i = 0; i < n; i++) lcp[i] = plcp[sa[i]];
}
int M[LG][MX], logTable[MX];
void pre(int n, int A[]) {
  for (int i = 2; i <= n; i++) logTable[i] = logTable[i >> 1] + 1;
  for (int i = 0; i < n; i++) M[0][i] = A[i];
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 0; i < n - (1 << j) + 1; i++)
      M[j][i] = ((M[j - 1][i]) < (M[j - 1][i + (1 << (j - 1))])
                     ? (M[j - 1][i])
                     : (M[j - 1][i + (1 << (j - 1))]));
}
int k;
inline int query(int i, int j) {
  if (i > j) return 1e9;
  k = logTable[j - i + 1];
  return ((M[k][i]) < (M[k][j - (1 << k) + 1]) ? (M[k][i])
                                               : (M[k][j - (1 << k) + 1]));
}
int n, m, tam[MX + 100005], cn[MX], ind[MX + 100005];
string in[100005], s;
int x[MX + 100005], y[MX + 100005], sz, q[MX + 100005];
inline int leq(int i, int j, int len) {
  int x = f[i], y = f[j];
  if (x < y && query(x + 1, y) < len) return 1;
  if (x > y && query(y + 1, x) < len) return -1;
  return 0;
}
int sza, szb, g, to;
bool comp(int i, int j) {
  sza = tam[i], szb = tam[j];
  g = leq(x[i], x[j], ((y[i]) < (y[j]) ? (y[i]) : (y[j])));
  if (g == 1) return 1;
  if (g == -1) return 0;
  if (y[i] < y[j])
    g = leq(x[i] + y[i] + 1, x[j] + y[i],
            ((y[j]) < (sza - 1) ? (y[j]) : (sza - 1)) - y[i]);
  if (y[i] > y[j])
    g = leq(x[i] + y[j], x[j] + y[j] + 1,
            ((y[i]) < (szb - 1) ? (y[i]) : (szb - 1)) - y[j]);
  if (g == 1) return 1;
  if (g == -1) return 0;
  to = y[i];
  if (to < y[j]) to = y[j];
  if (((sza) < (szb) ? (sza) : (szb)) > (to + 1))
    g = leq(x[i] + to + 1, x[j] + to + 1,
            ((sza) < (szb) ? (sza) : (szb)) - (to + 1));
  if (g == 1) return 1;
  if (g == -1) return 0;
  if (y[i] < sza) sza--;
  if (y[j] < szb) szb--;
  if (sza == szb) return x[i] < x[j];
  return sza < szb;
}
int dp[2][MX];
vector<int> ord[100005];
void sum(int &a, int b) {
  a += b;
  if (a >= mod) a -= mod;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> in[i];
    for (int j = 0; j < in[i].size() + 1; j++) {
      x[sz] = s.size();
      y[sz] = j;
      tam[sz] = in[i].size();
      ind[sz] = i;
      sz++;
    }
    s += in[i];
  }
  m = s.size();
  for (int i = 0; i < m; i++) S[i] = s[i] - 'a' + 1;
  obtSA(S, sa, m, 26);
  obtLCP(s);
  pre(m, lcp);
  for (int i = 0; i < m; i++) f[sa[i]] = i;
  sz = 0;
  for (int i = 0; i < n; i++) {
    ord[i].resize(in[i].size() + 1);
    int l = 0, r = (int)in[i].size(), j = 0;
    while (j < in[i].size()) {
      int to = j;
      while (to < in[i].size() && in[i][j] == in[i][to]) to++;
      while (j < to) {
        if (to == in[i].size() || in[i][j] > in[i][to])
          ord[i][l++] = sz + j++;
        else
          ord[i][r--] = sz + j++;
      }
    }
    ord[i][l] = sz + in[i].size();
    sz += in[i].size() + 1;
  }
  int x = 0, y = 1;
  fill(dp[x], dp[x] + ord[0].size(), 1);
  for (int i = 0; i < n - 1; i++) {
    int j = 0;
    for (int k = 0; k < ord[i + 1].size(); k++) {
      if (k)
        dp[y][k] = dp[y][k - 1];
      else
        dp[y][k] = 0;
      while (j < ord[i].size() && comp(ord[i][j], ord[i + 1][k]))
        sum(dp[y][k], dp[x][j++]);
    }
    swap(x, y);
  }
  int res = 0;
  for (int i = 0; i < ord[n - 1].size(); i++) sum(res, dp[x][i]);
  cout << res << '\n';
  return 0;
}