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 P = 1000000007;
const int maxn = 1000009;
const int base = 233;
long long pw[maxn];
int n;
string s[maxn];
vector<long long> h[maxn];
vector<int> p[maxn];
vector<long long> f[maxn];
int q[maxn];
int nex[maxn];
long long gethash(int i, int x, int a) {
  if (x < a) return h[i][x];
  ++x;
  long long L = (a ? h[i][a - 1] : 0) * pw[x - a] % P;
  long long R = h[i][x] - h[i][a] * pw[x - a] % P + P;
  return (L + R) % P;
}
int cmp(int i, int a, int j, int b) {
  int len1 = s[i].length() - (a != s[i].length());
  int len2 = s[j].length() - (b != s[j].length());
  int t = -1;
  for (int k = 19; k >= 0; --k) {
    if (t + (1 << k) >= min(len1, len2)) continue;
    int r = t + (1 << k);
    if (gethash(i, r, a) != gethash(j, r, b)) continue;
    t += 1 << k;
  }
  len1 = s[i].length();
  len2 = s[j].length();
  int l = t + 1, r = t + 1;
  if (a <= l) ++l;
  if (b <= r) ++r;
  l = (l >= len1) ? 0 : s[i][l];
  r = (r >= len2) ? 0 : s[j][r];
  if (l < r) return -1;
  if (l == r) return 0;
  if (l > r) return 1;
}
int main() {
  pw[0] = 1;
  for (int i = 1; i <= 1000000; ++i) pw[i] = pw[i - 1] * base % P;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    int len = s[i].length();
    long long pre = 0;
    for (int j = 0; j < len; ++j) {
      pre = (pre * base + s[i][j]) % P;
      h[i].push_back(pre);
    }
    int l = 1, r = len + 1;
    nex[len - 1] = 0;
    for (int j = len - 2; j >= 0; --j) {
      if (s[i][j] == s[i][j + 1])
        nex[j] = nex[j + 1];
      else
        nex[j] = s[i][j + 1];
    }
    for (int j = 0; j <= len - 1; ++j) {
      if (s[i][j] > nex[j])
        q[l++] = j;
      else
        q[r--] = j;
    }
    q[l] = len;
    for (int j = 1; j <= len + 1; ++j) p[i].push_back(q[j]);
  }
  for (int i = 0; i < p[1].size(); ++i) f[1].push_back(1);
  for (int i = 2; i <= n; ++i) {
    long long pre = 0, t = 0;
    for (int j = 0; j < p[i].size(); ++j) {
      while (t < p[i - 1].size() && cmp(i - 1, p[i - 1][t], i, p[i][j]) <= 0)
        pre = (pre + f[i - 1][t++]) % P;
      f[i].push_back(pre);
    }
  }
  long long ans = 0;
  for (int j = 0; j < p[n].size(); ++j) ans = (ans + f[n][j]) % P;
  printf("%lld\n", ans);
  return 0;
}