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;
int maxn = 1e5, maxl = 1e6 + 1;
long long k = 57, mod = 1791791791;
vector<vector<long long>> hs(maxn);
vector<long long> pw(maxl, 1);
long long hsh(int i, int l, int r) {
  return (mod + hs[i][r] - ((hs[i][l] * pw[r - l]) % mod)) % mod;
}
long long get_hs(int i, int del, int r) {
  if (r <= del)
    return hs[i][r];
  else
    return (((hs[i][del] * pw[r - del]) % mod) + hsh(i, del + 1, r + 1)) % mod;
}
int get(int ind, int del) {
  if (ind < del) return ind;
  return ind + 1;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 1; i < maxl; i++) {
    pw[i] = (pw[i - 1] * k) % mod;
  }
  int n;
  cin >> n;
  vector<string> a(n);
  long long modd = 1e9 + 7;
  vector<vector<long long>> dp(n);
  vector<vector<int>> srt(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    int f = a[i].size();
    dp[i].resize(f + 1);
    hs[i].resize(f + 1);
    for (int j = 0; j < f; j++) {
      hs[i][j + 1] = (hs[i][j] * k + a[i][j]) % mod;
    }
    vector<int> next(f, -1);
    vector<int> lft, rght;
    for (int j = 1; j < f; j++) {
      if (a[i][j] != a[i][j - 1]) {
        int id = j - 1;
        while (id >= 0 && a[i][j - 1] == a[i][id]) {
          next[id] = j;
          id--;
        }
      }
    }
    for (int j = 0; j < f; j++) {
      if (next[j] == -1 || a[i][next[j]] < a[i][j]) {
        lft.push_back(j);
      } else {
        rght.push_back(j);
      }
    }
    reverse(rght.begin(), rght.end());
    for (auto el : lft) srt[i].push_back(el);
    srt[i].push_back(f);
    for (auto el : rght) srt[i].push_back(el);
    if (i == 0) {
      for (int j = 0; j <= f; j++) {
        dp[i][j] = 1;
      }
    } else {
      int ind_i = 0, ind_j = 0;
      long long sm = 0;
      while (ind_i < srt[i].size() && ind_j < srt[i - 1].size()) {
        int len_i = a[i].size() - 1;
        if (srt[i][ind_i] == a[i].size()) len_i++;
        int len_j = a[i - 1].size() - 1;
        if (srt[i - 1][ind_j] == a[i - 1].size()) len_j++;
        int l = 0, r = min(len_i, len_j) + 1;
        while (r - l > 1) {
          int m = (l + r) / 2;
          if (get_hs(i, srt[i][ind_i], m) ==
              get_hs(i - 1, srt[i - 1][ind_j], m))
            l = m;
          else
            r = m;
        }
        if (l == len_j ||
            (l != len_i && a[i][get(l, srt[i][ind_i])] >
                               a[i - 1][get(l, srt[i - 1][ind_j])])) {
          sm += dp[i - 1][srt[i - 1][ind_j]];
          sm %= modd;
          ind_j++;
        } else {
          dp[i][srt[i][ind_i]] += sm;
          dp[i][srt[i][ind_i]] %= modd;
          ind_i++;
        }
      }
      while (ind_i < srt[i].size()) {
        dp[i][srt[i][ind_i]] += sm;
        dp[i][srt[i][ind_i]] %= modd;
        ind_i++;
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < dp[n - 1].size(); i++) {
    ans += dp[n - 1][i];
    ans %= modd;
  }
  cout << ans << endl;
  return 0;
}