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 mod = 1000000007;
const int mxn = 1000001;
int n, m;
string second, t, ss, tt;
int a[mxn], b[mxn], c[mxn], first[mxn], g[mxn], dp[mxn], dp2[mxn];
void bld() {
  m = max(second.size(), t.size()) + 1;
  second.resize(m + 1, '#'), t.resize(m + 1, '#');
  a[m] = b[m] = b[m - 1] = c[m] = c[m - 1] = 0;
  for (int i = m - 1; ~i; i--) {
    a[i] = (second[i] == t[i]) * (a[i + 1] + 1);
    if (i < m - 1) {
      b[i] = (second[i] == t[i + 1]) * (b[i + 1] + 1);
      c[i] = (second[i + 1] == t[i]) * (c[i + 1] + 1);
    }
  }
}
bool cmp(int x, int y) {
  int z = max(x, y);
  if (a[0] < min(x, y)) return second[a[0]] < t[a[0]];
  if (x > y && b[y] < x - y) return second[y + b[y]] < t[y + b[y] + 1];
  if (x < y && c[x] < y - x) return second[x + c[x] + 1] < t[x + c[x]];
  return second[z + a[z + 1] + 1] < t[z + a[z + 1] + 1];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    swap(ss, tt);
    memcpy(g, first, (tt.size() + 1) * sizeof(int));
    memcpy(dp2, dp, (tt.size() + 1) * sizeof(int));
    cin >> ss;
    second = t = ss, bld();
    for (int j = 0, l = 0, r = m - 1; j < m; j++) {
      if (second[j + b[j] + 1] < second[j])
        first[l++] = j;
      else
        first[r--] = j;
    }
    second = ss, t = tt, bld();
    for (int j = 0, l = 0; j <= ss.size(); j++) {
      while (l <= tt.size() && !cmp(first[j], g[l])) l++;
      dp[j] = ((j ? dp[j - 1] : 0) + (l ? dp2[l - 1] : 0)) % mod;
    }
  }
  cout << dp[ss.size()] << '\n';
  return 0;
}