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>
inline int fp(int x, int k) {
  int res = 1;
  for (; k; k >>= 1, x = 1ll * x * x % 1000000007)
    if (k & 1) res = 1ll * res * x % 1000000007;
  return res;
}
int n, m;
char str[1000005];
std::vector<int> s[100005];
std::vector<int> pos[100005], nxt;
const int X = 37, iX = fp(X, 1000000007 - 2);
std::vector<int> hsh[100005];
std::vector<int> f[100005];
inline void init(int x) {
  f[x].resize(s[x].size());
  pos[x].resize(s[x].size());
  nxt.resize(s[x].size());
  hsh[x].resize(s[x].size());
  nxt[s[x].size() - 1] = s[x].size() - 1;
  for (int i = s[x].size() - 2; i >= 0; i--)
    nxt[i] = s[x][i] == s[x][i + 1] ? nxt[i + 1] : i + 1;
  int l = 0, r = s[x].size() - 1;
  for (int i = 0; i < s[x].size(); i++)
    if (s[x][i] > s[x][nxt[i]])
      pos[x][l++] = i;
    else
      pos[x][r--] = i;
  hsh[x][0] = s[x][0];
  for (int i = 1, tmp = X; i < s[x].size();
       i++, tmp = 1ll * tmp * X % 1000000007)
    hsh[x][i] = (hsh[x][i - 1] + 1ll * s[x][i] * tmp % 1000000007) % 1000000007;
}
inline int gethsh(int x, int i, int j) {
  return j < i ? hsh[x][j]
               : (1ll * (hsh[x][j + 1] - hsh[x][i] + 1000000007) * iX %
                      1000000007 +
                  (i ? hsh[x][i - 1] : 0)) %
                     1000000007;
}
inline bool cmp(int x, int i, int y, int j) {
  int l = 0, r = std::min(s[x].size(), s[y].size()) - 2, k = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (gethsh(x, i, mid) == gethsh(y, j, mid)) {
      k = mid + 1;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  if (k == std::min(s[x].size(), s[y].size()) - 1)
    return s[x].size() <= s[y].size() ||
           (s[x].size() - 1 == s[y].size() && j == s[y].size() - 1 &&
            i < s[x].size() - 1);
  return s[x][k + (k >= i)] < s[y][k + (k >= j)];
}
int ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", str);
    m = strlen(str);
    for (int j = 0; j < m; j++) s[i].push_back(str[j] - 'a' + 2);
    s[i].push_back(1);
    init(i);
  }
  for (int i = 0; i < s[1].size(); i++) f[1][i] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0, k = 0, tmp = 0; j < s[i].size(); j++) {
      while (k < s[i - 1].size() && cmp(i - 1, pos[i - 1][k], i, pos[i][j])) {
        tmp = (tmp + f[i - 1][pos[i - 1][k]]) % 1000000007;
        k++;
      }
      f[i][pos[i][j]] = tmp;
    }
  }
  for (int i = 0; i < s[n].size(); i++)
    ans = (ans + f[n][i] % 1000000007) % 1000000007;
  printf("%d\n", ans);
}