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 const maxn = 1e5 + 5, maxc = 1e6 + 5;
long long mod[2], P[2], p[2][maxc], rev_P[2];
vector<long long> h[2][maxn];
vector<int> sorted[maxn];
string s[maxn];
int nxt[maxc];
int a[maxc], dp[2][maxc], inf = 1e9 + 7;
int MOD = 1e9 + 7;
long long st(long long x, int y, int ok) {
  if (y == 0) return 1;
  if (y % 2 == 0) {
    long long d = st(x, y / 2, ok);
    return d * d % mod[ok];
  }
  return x * st(x, y - 1, ok) % mod[ok];
}
inline char get_c(int i, int x, int numb) {
  if (numb < x) return s[i][numb];
  if (numb + 1 < (int)s[i].size()) return s[i][numb + 1];
  return ' ';
}
inline long long get_hash(int t, int i, int x, int len) {
  if (len < x) return h[t][i][len];
  return (h[t][i][x] +
          (h[t][i][len + 1] - h[t][i][x + 1] + mod[t]) * rev_P[t]) %
         mod[t];
}
inline pair<long long, long long> get_h(int i, int x, int len) {
  return {get_hash(0, i, x, len), get_hash(1, i, x, len)};
}
inline int check(int i, int x, int j, int y) {
  int len1 = (int)s[i].size(), len2 = (int)s[j].size();
  if (x != len1) len1--;
  if (y != len2) len2--;
  int lef = 0, righ = min(len1, len2) + 1;
  while (righ - lef > 1) {
    int mid = (righ + lef) / 2;
    if (get_h(i, x, mid) == get_h(j, y, mid))
      lef = mid;
    else
      righ = mid;
  }
  return get_c(i, x, lef) >= get_c(j, y, lef);
}
int main() {
  mod[0] = 1e9 + 7, mod[1] = 1e9 + 9, P[0] = 29, P[1] = 31,
  rev_P[0] = st(P[0], mod[0] - 2, 0), rev_P[1] = st(P[1], mod[1] - 2, 1);
  p[0][0] = 1, p[1][0] = 1;
  for (int i = 1; i < maxc; ++i) {
    for (int j = 0; j <= 1; ++j) p[j][i] = p[j][i - 1] * P[j] % mod[j];
  }
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    for (int j = 0; j <= 1; ++j) {
      h[j][i].push_back(0);
      for (int pos = 0; pos < (int)s[i].size(); ++pos) {
        h[j][i].push_back((h[j][i][pos] + p[j][pos] * (s[i][pos] - 'a' + 1)) %
                          mod[j]);
      }
    }
    nxt[(int)s[i].size() - 1] = (int)s[i].size() - 1;
    for (int pos = (int)s[i].size() - 2; pos >= 0; --pos) {
      if (s[i][pos] != s[i][pos + 1])
        nxt[pos] = pos + 1;
      else
        nxt[pos] = nxt[pos + 1];
    }
    int l = 0, r = (int)s[i].size() - 1;
    for (int j = 0; j < (int)s[i].size(); ++j) {
      if (s[i][nxt[j]] <= s[i][j])
        a[l++] = j;
      else
        a[r--] = j;
    }
    for (int j = 0; j < (int)s[i].size(); ++j) {
      sorted[i].push_back(a[j]);
      if (a[j] == (int)s[i].size() - 1) sorted[i].push_back((int)s[i].size());
    }
  }
  for (int i = 0; i <= (int)s[1].size(); ++i) {
    dp[0][i] = 1;
  }
  for (int i = 2; i <= n; ++i) {
    int oks = (i - 1) % 2, ptr = 0, sum = 0, cur = -1;
    for (auto key : sorted[i]) {
      cur++;
      while (ptr < (int)sorted[i - 1].size() &&
             check(i, key, i - 1, sorted[i - 1][ptr])) {
        sum += dp[(1 ^ oks)][ptr];
        if (sum >= MOD) sum -= MOD;
        ptr++;
      }
      dp[oks][cur] = sum;
    }
  }
  int ans = 0;
  for (int i = 0; i <= (int)s[n].size(); ++i) {
    ans += dp[(n - 1) % 2][i];
    if (ans >= MOD) ans -= MOD;
  }
  cout << ans << '\n';
  return 0;
}