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 unsigned _mod = 998244353;
const unsigned mod = 1e9 + 7;
const long long infi = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long qpow(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1ll) ret = ret * x;
    y >>= 1ll;
    x = x * x;
  }
  return ret;
}
long long ksm(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1ll) ret = ret * x % mod;
    y >>= 1ll;
    x = x * x % mod;
  }
  return ret;
}
long long _gcd(long long x, long long y) { return y ? _gcd(y, x % y) : x; }
int n, x, y, f, ans, A[2][1000010], dp[2][1000010], X[1000010], Y[1000010],
    Z[1000010];
string s, t, S[2];
void build() {
  f = (s.size() > t.size() ? s.size() : t.size());
  s.resize(f + 1, '$'), t.resize(f + 1, '$');
  X[f] = Y[f - 1] = Z[f - 1] = 0;
  for (int i = f - 1; i >= 0; --i) X[i] = (s[i] == t[i]) * (X[i + 1] + 1);
  for (int i = f - 2; i >= 0; --i) Y[i] = (s[i] == t[i + 1]) * (Y[i + 1] + 1);
  for (int i = f - 2; i >= 0; --i) Z[i] = (s[i + 1] == t[i]) * (Z[i + 1] + 1);
}
bool cmp(int x, int y) {
  if (x == -1 && y == -1) return (X[0] < f && s[X[0]] < t[X[0]]);
  if (x == -1)
    return (X[0] < y ? s[X[0]] < t[X[0]] : s[Y[y] + y] < t[Y[y] + y + 1]);
  if (y == -1)
    return (X[0] < x ? s[X[0]] < t[X[0]] : s[Z[x] + x + 1] < t[Z[x] + x]);
  if (X[0] < (x < y ? x : y)) return s[X[0]] < t[X[0]];
  if (x <= y && Z[x] < y - x) return s[Z[x] + x + 1] < t[Z[x] + x];
  if (x > y && Y[y] < x - y) return s[Y[y] + y] < t[Y[y] + y + 1];
  int mx = (x > y ? x : y) + 1;
  return s[X[mx] + mx] < t[X[mx] + mx];
}
int main() {
  cin >> n;
  A[0][0] = -1, dp[0][0] = 1;
  if (n == 1) {
    cin >> s;
    return cout << s.size() + 1 << '\n', 0;
  }
  for (int _ = 1; _ <= n; ++_) {
    int x = _ & 1;
    cin >> S[x], s = t = S[x];
    build();
    for (int i = 0; i <= f; ++i) A[x][i] = i - 1;
    sort(A[x], A[x] + f + 1, cmp);
    s = S[x], t = S[x ^ 1];
    build();
    int sum = 0, j = 0;
    for (int i = 0; i <= f; ++i) {
      while (j <= S[x ^ 1].size() && !cmp(A[x][i], A[x ^ 1][j]))
        sum = (sum + dp[x ^ 1][j++]) % mod;
      dp[x][i] = sum;
    }
  }
  for (int i = 0; i <= S[n & 1].size(); ++i) ans = (ans + dp[n & 1][i]) % mod;
  cout << ans << "\n";
  return 0;
}