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 + 5, C = 1e6 + 5;
int n;
long long mod[2], P[2], p[2][C], rev_P[2];
vector<long long> h[2][N];
vector<int> sorted[N];
string s[N];
int nxt[C];
int a[C], dp[2][C];
const int MOD = (int)1e9 + 7;
inline void inc(int &x, int y) { x = x + y < MOD ? x + y : x + y - MOD; }
inline long long quickpow(long long a, int n, int hm) {
  long long res = 1;
  for (; n; n >>= 1, a = (long long)(a * a) % mod[hm])
    if (n & 1) res = (long long)(res * a) % mod[hm];
  return res;
}
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 gethash(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(int i, int x, int len) {
  return {gethash(0, i, x, len), gethash(1, i, x, len)};
}
inline bool 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, rht = min(len1, len2) + 1;
  while (rht - lef > 1) {
    int mid = (lef + rht) >> 1;
    if (get(i, x, mid) == get(j, y, mid))
      lef = mid;
    else
      rht = mid;
  }
  return get_c(i, x, lef) >= get_c(j, y, lef);
}
int main() {
  mod[0] = (int)1e9 + 7;
  mod[1] = (int)1e9 + 9;
  P[0] = 31;
  P[1] = 33;
  rev_P[0] = quickpow(P[0], mod[0] - 2, 0),
  rev_P[1] = quickpow(P[1], mod[1] - 2, 1);
  p[0][0] = 1;
  p[1][0] = 1;
  for (int i = 1; i < C; ++i)
    for (int j = 0; j <= 1; ++j)
      p[j][i] = (long long)p[j][i - 1] * P[j] % mod[j];
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    for (int j = 0; j <= 1; ++j) {
      h[j][i].emplace_back(0);
      for (unsigned pos = 0; pos < s[i].size(); ++pos)
        h[j][i].emplace_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].emplace_back(a[j]);
      if (a[j] == (int)s[i].size() - 1)
        sorted[i].emplace_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 ptr = 0, sum = 0, cur = -1, now = (i - 1) & 1;
    for (auto key : sorted[i]) {
      ++cur;
      while (ptr < (int)sorted[i - 1].size() &&
             check(i, key, i - 1, sorted[i - 1][ptr])) {
        inc(sum, dp[now ^ 1][ptr]);
        ++ptr;
      }
      dp[now][cur] = sum;
    }
  }
  int ans = 0;
  for (int i = 0; i <= (int)s[n].size(); ++i) inc(ans, dp[(n - 1) & 1][i]);
  printf("%d\n", ans);
  return 0;
}