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 = 1e6 + 2, mod[2] = {(int)1e9 + 7, (int)1e9 + 9},
          base[2] = {31, 37};
int n, dp[N][2], prvh[N][2], curh[N][2], p[N][2], nxt[N];
string s, prvs;
void hashstring(int h[][2]) {
  for (int i = 1; i <= s.size(); i++)
    for (int j = 0; j <= 1; j++)
      h[i][j] = (1LL * h[i - 1][j] * base[j] + s[i - 1] - 'a' + 1) % mod[j];
}
void getnext() {
  int sz = s.size();
  nxt[sz - 1] = sz - 1;
  for (int i = sz - 2; i >= 0; i--)
    nxt[i] = s[i] == s[i + 1] ? nxt[i + 1] : i + 1;
}
bool sortt(int i, int j) {
  if (max(i, j) == s.size()) {
    if (i < j) {
      if (s[nxt[i]] == s[i]) return true;
      return s[nxt[i]] < s[nxt[i] - 1];
    }
    if (s[nxt[j]] == s[j]) return false;
    return s[nxt[j] - 1] < s[nxt[j]];
  }
  if (i < j) return s[nxt[i]] < s[nxt[i] + (j < nxt[i]) - 1];
  return s[nxt[j] + (i < nxt[j]) - 1] < s[nxt[j]];
}
int gethash(int t, int h[][2], int len, int i) {
  if (i > len) return h[len][t];
  len++;
  int h1 = 1LL * h[i - 1][t] * p[len - i][t] % mod[t];
  int h2 = (1LL * h[len][t] - 1LL * h[i][t] * p[len - i][t]) % mod[t];
  return (1LL * h1 + h2 + mod[t]) % mod[t];
}
pair<int, int> gethash(int h[][2], int len, int i) {
  return {gethash(0, h, len, i), gethash(1, h, len, i)};
}
bool good(int i, int n, int j, int m) {
  i++, j++;
  int low = 0, high = min(n - (i <= n), m - (j <= m));
  int id = -1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (gethash(prvh, mid, i) == gethash(curh, mid, j))
      low = mid + 1;
    else
      id = mid, high = mid - 1;
  }
  if (id == -1) return (n - (i <= n)) <= (m - (j <= m));
  return prvs[id + (i <= id) - 1] <= s[id + (j <= id) - 1];
}
vector<int> prv, cur;
int prvsz, cursz;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  p[0][0] = p[0][1] = 1;
  for (int i = 1; i <= 1e6; i++)
    for (int j = 0; j < 2; j++) p[i][j] = 1LL * p[i - 1][j] * base[j] % mod[j];
  cin >> n >> s;
  prvsz = s.size() + 1;
  prv.resize(1e6 + 1);
  cur.reserve(1e6 + 1);
  for (int i = 0; i <= s.size(); i++) prv[i] = i;
  hashstring(prvh);
  getnext();
  sort(prv.begin(), prv.begin() + prvsz, sortt);
  for (int i = 1; i <= prvsz; i++) dp[i][1] = 1 + dp[i - 1][1];
  for (int i = 2; i <= n; i++) {
    prvs = s;
    cin >> s;
    cursz = s.size() + 1;
    for (int j = 0; j <= s.size(); j++) cur[j] = j;
    hashstring(curh);
    getnext();
    sort(cur.begin(), cur.begin() + cursz, sortt);
    int low = 0, high = prvsz;
    for (int j = 0; j < cursz; j++) {
      while (low != high && good(prv[low], prvsz - 1, cur[j], cursz - 1)) low++;
      dp[j + 1][i % 2] = (1LL * dp[low][(i + 1) % 2] + dp[j][i % 2]) % mod[0];
    }
    prvsz = cursz;
    for (int j = 0; j < prvsz; j++) prv[j] = cur[j];
    for (int a = 1; a < cursz; a++)
      for (int b = 0; b < 2; b++) prvh[a][b] = curh[a][b];
  }
  cout << dp[prvsz][n % 2];
}