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>
const int maxn = 1e5 + 5;
const int p = 131;
const int mo = 998244353;
const int Mo = 1e9 + 7;
const int lim = 1e6;
const int maxm = 1e6 + 5;
using namespace std;
int n, f[2][maxm], nxt[maxm];
string str[maxn];
int s[2][maxm];
long long pw[maxm], H[2][maxm];
inline long long hash_del(int x, int pos, int del) {
  int id = x & 1;
  if (pos < del) return H[id][pos];
  long long now = 0;
  if (del > 0) now = H[id][del - 1];
  now = now * pw[pos + 1 - del] % mo;
  now = (now + H[id][pos + 1] - pw[pos + 1 - del] * H[id][del]) % mo;
  if (now < 0) now += mo;
  return now;
}
inline char get_c(int x, int pos, int del) {
  if (pos < del) return str[x][pos];
  if (((int)str[x].size()) > pos + 1) return str[x][pos + 1];
  return 'a' - 1;
}
inline bool comepare(int x, int del_x, int y, int del_y) {
  int len_x = ((int)str[x].size()), len_y = ((int)str[y].size()), l = 0,
      r = min(len_x, len_y) + 1;
  for (; r - l > 1;) {
    int mid = (l + r) >> 1;
    if (hash_del(x, mid - 1, del_x) != hash_del(y, mid - 1, del_y))
      r = mid;
    else
      l = mid;
  }
  return get_c(x, l, del_x) <= get_c(y, l, del_y);
}
inline void get(int x) {
  int len = ((int)str[x].size()), id = x & 1;
  for (int i = len - 1; i >= 0; i--)
    nxt[i] = (str[x][i] != str[x][i + 1]) ? i + 1 : nxt[i + 1];
  H[id][0] = str[x][0];
  for (int i = 1; i <= len; i++) H[id][i] = (H[id][i - 1] * p + str[x][i]) % mo;
  int l = 0, r = len - 1;
  for (int i = 0; i < len; i++) {
    if (str[x][i] > str[x][nxt[i]])
      s[id][l++] = i;
    else
      s[id][r--] = i;
  }
  for (int i = len; i >= 0; i--) {
    if (s[id][i - 1] != len - 1)
      s[id][i] = s[id][i - 1];
    else {
      s[id][i] = len;
      break;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  pw[0] = 1;
  for (int i = 1; i <= lim + 1; i++) pw[i] = pw[i - 1] * p % mo;
  for (int i = 1; i <= n; i++) cin >> str[i];
  for (int i = 1; i <= n; i++) {
    int len = ((int)str[i].size()), x = i & 1,
        len_last = ((int)str[i - 1].size()), l = -1, sum = 0;
    get(i);
    for (int j = 0; j <= len; j++) {
      if (i == 1) {
        f[x][j] = 1;
        continue;
      }
      for (; l < len_last && comepare(i - 1, s[x ^ 1][l + 1], i, s[x][j]);) {
        ++l;
        sum += f[x ^ 1][l];
        if (sum >= Mo) sum -= Mo;
      }
      if (l != -1)
        f[x][j] = sum;
      else
        f[x][j] = 0;
    }
  }
  int ans = 0, len = ((int)str[n].size());
  for (int i = 0; i <= len; i++) {
    ans += f[n & 1][i];
    if (ans >= Mo) ans -= Mo;
  }
  cout << ans;
  return 0;
}