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>
#pragma GCC optimize("Ofast")
using namespace std;
template <class T, class U>
bool cmax(T& a, const U& b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class U>
bool cmin(T& a, const U& b) {
  return b < a ? a = b, 1 : 0;
}
void _BG(const char* s) { cerr << s << endl; }
template <class T, class... TT>
void _BG(const char* s, T a, TT... b) {
  for (size_t c = 0; *s && (c || *s != ','); cerr << *s++)
    c += count(begin("([{"), end("([{"), *s) -
         count(begin(")]}"), end(")]}"), *s);
  cerr << " = " << a;
  if (*s) {
    cerr << ", ";
    ++s;
  }
  _BG(s, b...);
}
const int N = 1e6 + 87, MOD = 1e9 + 7;
const int W = 1;
const array<int, W> HMOD = {1010101333}, HP = {52127};
array<int, W> pw[N];
string s[N];
vector<int> dp[N], nxt[N];
vector<array<int, W> > h[N];
vector<pair<int, int> > vp[N];
array<int, W> operator*(array<int, W> a, array<int, W> b) {
  array<int, W> c;
  for (int i = 0; i < W; ++i) c[i] = a[i] * 1ll * b[i] % HMOD[i];
  return c;
}
array<int, W> operator+(array<int, W> a, array<int, W> b) {
  array<int, W> c;
  for (int i = 0; i < W; ++i) c[i] = (a[i] + b[i]) % HMOD[i];
  return c;
}
array<int, W> operator-(array<int, W> a, array<int, W> b) {
  array<int, W> c;
  for (int i = 0; i < W; ++i) c[i] = (a[i] + HMOD[i] - b[i]) % HMOD[i];
  return c;
}
array<int, W> operator+(array<int, W> a, int x) {
  array<int, W> c;
  for (int i = 0; i < W; ++i) c[i] = (a[i] + x) % HMOD[i];
  return c;
}
array<int, W> hh(int i, int j, int len) {
  return h[i][j] - h[i][j + len] * pw[len];
}
array<int, W> f(pair<int, int> a, int len) {
  if (len <= a.second)
    return hh(a.first, 0, len);
  else
    return hh(a.first, 0, a.second) +
           hh(a.first, a.second + 1, len - a.second) * pw[a.second];
}
char g(pair<int, int> a, int i) {
  return i < a.second ? s[a.first][i] : s[a.first][i + 1];
}
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first) {
    int i = nxt[a.first][a.second], j = nxt[a.first][b.second];
    if (i == j) return false;
    if (i < j)
      return s[a.first][i] < s[a.first][i - 1];
    else
      return s[a.first][j - 1] < s[a.first][j];
  } else {
    int ls =
        ((int)(s[a.first]).size()) - (a.second < ((int)(s[a.first]).size()));
    int rs =
        ((int)(s[b.first]).size()) - (b.second < ((int)(s[b.first]).size()));
    int len = min(ls, rs);
    if (f(a, len) == f(b, len)) return ls < rs;
    int lo = 1, hi = len - 1;
    while (lo <= hi) {
      int mi = (lo + hi) / 2;
      if (f(a, mi) == f(b, mi))
        lo = mi + 1;
      else
        hi = mi - 1;
    }
    return g(a, hi) < g(b, hi);
  }
}
string gs(pair<int, int> a) {
  return s[a.first].substr(0, a.second) + s[a.first].substr(a.second + 1);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  assert(HMOD.size() == HP.size());
  pw[0].fill(1);
  for (int i = 1; i < N; ++i) pw[i] = pw[i - 1] * HP;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    int m = s[i].size();
    dp[i] = nxt[i] = vector<int>(m + 1);
    nxt[i][m - 1] = m;
    for (int j = m - 2; j >= 0; --j)
      if (s[i][j + 1] == s[i][j])
        nxt[i][j] = nxt[i][j + 1];
      else
        nxt[i][j] = j + 1;
    h[i].assign(m + 1, {});
    for (int j = m - 1; j >= 0; --j) h[i][j] = h[i][j + 1] * HP + s[i][j];
    vp[i].resize(m + 1);
    int l = 0, r = m;
    for (int j = 0; j <= m; ++j)
      if (s[i][nxt[i][j]] < s[i][j])
        vp[i][l++] = pair<int, int>(i, j);
      else
        vp[i][r--] = pair<int, int>(i, j);
    if (i == 0) {
      fill(begin(dp[i]), end(dp[i]), 1);
    } else {
      int su = 0;
      for (int j = 0, k = 0; j <= m; ++j) {
        while (k < vp[i - 1].size() && !cmp(vp[i][j], vp[i - 1][k])) {
          su = (su + dp[i - 1][k++]) % MOD;
        }
        dp[i][j] = su;
      }
    }
  }
  cout << accumulate(begin(dp[n - 1]), end(dp[n - 1]), 0ll) % MOD << endl;
}