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 = 1e5 + 1;
const int M = 1e6 + 1;
const int mod = 1e9 + 7;
const int base = 41;
int n, sz[N];
string s[N];
vector<int> f[N], lcp[N];
long long g[M], h[M], t[M], w[M];
vector<long long> hashArr[N];
long long p[M];
long long getHash(int i, int l, int r) {
  return ((hashArr[i][r] - hashArr[i][l - 1] * p[r - l + 1]) % mod + mod) % mod;
}
int getFirstDiff(int i, int l1, int r1, int l2, int r2) {
  int r0 = min(r1 - l1, r2 - l2) + l1, v = l1 - 1;
  int l = l1, r = r0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (getHash(i, l1, mid) == getHash(i + 1, l2, l2 + mid - l1)) {
      v = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  ++v;
  return v;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    sz[i] = s[i].size();
    s[i] = s[i] + " ";
    for (int j = 0; j <= sz[i]; ++j) f[i].push_back(0LL);
    hashArr[i].push_back(0LL);
    for (int j = 0; j < sz[i]; ++j) {
      hashArr[i].push_back((hashArr[i].back() * base + s[i][j] - 'a' + 1) %
                           mod);
    }
  }
  p[0] = 1;
  for (int i = 1; i < M; ++i) p[i] = (p[i - 1] * base) % mod;
  for (int i = 0; i <= s[1].size(); ++i) f[1][i] = 1;
  for (int i = 1; i <= n; ++i) {
    if (i > 1)
      for (int j = 1; j <= sz[i]; ++j) {
        long long x = g[j] + (j < lcp[i][j] ? t[lcp[i][j]] : 0);
        if (j < lcp[i][j]) x += w[lcp[i][j] - 1] - w[j];
        if (s[i][lcp[i][j] - 1] < s[i][lcp[i][j]]) x += h[lcp[i][j]];
        f[i][j] = (f[i][j] + x % mod) % mod;
      }
    if (i >= n) break;
    for (int j = 0; j <= sz[i + 1]; ++j) g[j] = h[j] = t[j] = w[j] = 0;
    lcp[i + 1].resize(sz[i + 1] + 1);
    lcp[i + 1][sz[i + 1]] = sz[i + 1];
    for (int j = sz[i + 1] - 1; j >= 1; --j) {
      if (s[i + 1][j - 1] == s[i + 1][j + 1 - 1])
        lcp[i + 1][j] = lcp[i + 1][j + 1];
      else
        lcp[i + 1][j] = j;
    }
    int k = getFirstDiff(i, 1, sz[i], 1, sz[i + 1]);
    for (int j = 0; j <= sz[i]; ++j) {
      int k0 = k;
      if (k0 >= j) k0 = getFirstDiff(i, j + 1, sz[i], max(j, 1), sz[i + 1]);
      int k1 = max(1, j <= 0 ? k0 : (k0 < j ? k0 : k0 - 1));
      if (s[i][k0 - 1] <= s[i + 1][k1 - 1]) {
        f[i + 1][0] = (f[i + 1][0] + f[i][j]) % mod;
        if (k1 < sz[i + 1]) {
          g[k1 + 1] += f[i][j];
        }
      }
      int k2 = 0;
      if (k0 > j)
        k2 = getFirstDiff(i, k0, sz[i], k1 + 1, sz[i + 1]);
      else {
        k2 = getFirstDiff(i, k0, j - 1, k1 + 1, sz[i + 1]);
        if (k2 >= j) {
          k2 = getFirstDiff(i, j + 1, sz[i], j + 1, sz[i + 1]);
        }
      }
      if (s[i][k2 - 1] <= s[i + 1][k2 + (k2 < j || j <= 0) - 1]) {
        f[i + 1][k1] = (f[i + 1][k1] + f[i][j]) % mod;
      }
      if (k1 > 1) {
        h[1] += f[i][j];
        h[k1] -= f[i][j];
      }
      if (s[i][k0 - 1] <= s[i + 1][k1 - 1]) {
        w[k1] += f[i][j];
      }
      if (s[i][k2 - 1] <= s[i + 1][k2 + (k2 < j || j <= 0) - 1]) {
        t[k1] += f[i][j];
      }
    }
    for (int j = 1; j <= sz[i + 1]; ++j) {
      g[j] += g[j - 1];
      h[j] += h[j - 1];
      w[j] += w[j - 1];
    }
  }
  int res = 0;
  for (int i = 0; i <= sz[n]; ++i) res = (res + f[n][i]) % mod;
  cout << res;
  return 0;
}