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 = 100005, B = 31, P = 998244853, Q = 1e9 + 7;
int n, pw[1000005];
vector<int> h[N], rk[N], f[N];
string s[N];
int get(const vector<int>& h, int l, int r) {
  return (h[l] - 1LL * h[r + 1] * pw[r - l + 1] % P + P) % P;
}
void fun(vector<int>& rk, const string& s) {
  vector<int> st1, st2;
  for (int i = 0, j; i < ((int)(s).size()); i = j) {
    j = i + 1;
    while (j < ((int)(s).size()) && s[i] == s[j]) ++j;
    if (j == ((int)(s).size()) || s[j] < s[i]) {
      for (int k = (i); k <= (j - 1); ++k) st1.emplace_back(k);
    } else {
      for (int k = (i); k <= (j - 1); ++k) st2.emplace_back(k);
    }
  }
  for (int i = (0); i <= (((int)(st1).size()) - 1); ++i)
    rk.emplace_back(st1[i]);
  rk.emplace_back(((int)(s).size()));
  for (int i = (((int)(st2).size()) - 1); i >= (0); --i)
    rk.emplace_back(st2[i]);
}
bool chk(const string& s1, const vector<int>& h1, int p1, const string& s2,
         const vector<int>& h2, int p2) {
  int i1 = 0, i2 = 0;
  static pair<int, int> rg1[2], rg2[2];
  rg1[0] = make_pair(0, p1 - 1),
  rg1[1] = make_pair(p1 + 1, ((int)(h1).size()) - 1 - 1);
  rg2[0] = make_pair(0, p2 - 1),
  rg2[1] = make_pair(p2 + 1, ((int)(h2).size()) - 1 - 1);
  while (i1 < 2 || i2 < 2) {
    while (i1 < 2 && rg1[i1].first > rg1[i1].second) ++i1;
    while (i2 < 2 && rg2[i2].first > rg2[i2].second) ++i2;
    if (i1 >= 2) return 1;
    if (i2 >= 2) return 0;
    int len = min(rg1[i1].second - rg1[i1].first + 1,
                  rg2[i2].second - rg2[i2].first + 1);
    if (get(h1, rg1[i1].first, rg1[i1].first + len - 1) !=
        get(h2, rg2[i2].first, rg2[i2].first + len - 1)) {
      int l = 1, r = len, res = 0;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (get(h1, rg1[i1].first, rg1[i1].first + mid - 1) ==
            get(h2, rg2[i2].first, rg2[i2].first + mid - 1)) {
          l = mid + 1, res = mid;
        } else
          r = mid - 1;
      }
      return s1[rg1[i1].first + res] < s2[rg2[i2].first + res];
    }
    rg1[i1].first += len, rg2[i2].first += len;
  }
  return 1;
}
int main() {
  pw[0] = 1;
  for (int i = (1); i <= (1000005 - 1); ++i) pw[i] = 1LL * pw[i - 1] * B % P;
  scanf("%d", &n);
  for (int i = (0); i <= (n - 1); ++i) {
    cin >> s[i];
    h[i].assign(((int)(s[i]).size()) + 1, 0);
    for (int j = ((int)(s[i]).size()) - 1; j >= 0; --j)
      h[i][j] = (1LL * h[i][j + 1] * B + s[i][j] - 'a' + 1) % P;
  }
  for (int i = (0); i <= (n - 1); ++i) {
    int sz = ((int)(s[i]).size());
    f[i].assign(sz + 1, 0);
    fun(rk[i], s[i]);
    if (i == 0) {
      for (int j = (0); j <= (sz); ++j) f[i][j] = 1;
    } else {
      int k = 0;
      for (int j = (0); j <= (sz); ++j) {
        while (k < ((int)(rk[i - 1]).size()) &&
               chk(s[i - 1], h[i - 1], rk[i - 1][k], s[i], h[i], rk[i][j]))
          ++k;
        if (k) f[i][j] = f[i - 1][k - 1];
      }
    }
    for (int j = (1); j <= (sz); ++j) (f[i][j] += f[i][j - 1]) %= Q;
  }
  printf("%d\n", f[n - 1][((int)(s[n - 1]).size())]);
  return 0;
}