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 mans = 1e9 + 7;
const int N = 1001000;
int n, f[2][N], ans, now;
int len[N], nxt[N], q[N];
char s[N];
int p[2], ip[2], mod[2], pw[N][2];
vector<int> c[N][2], a[N];
vector<char> t[N];
int power(int x, int len, int kd) {
  int res = 1;
  for (; len; len >>= 1, x = (long long)x * x % mod[kd])
    if (len & 1) res = (long long)res * x % mod[kd];
  return res;
}
char getc(int x, int dx, int k) {
  if (k < dx) return t[x][k - 1];
  if (k < len[x]) return t[x][k];
  return ' ';
}
int hsh(int x, int dx, int len, int kd) {
  if (len < dx) return c[x][kd][len];
  return (c[x][kd][dx - 1] +
          (long long)(c[x][kd][len + 1] - c[x][kd][dx] + mod[kd]) * ip[kd]) %
         mod[kd];
}
int check(int x, int dx, int y, int dy) {
  int l = 0,
      r = min(len[x] - (dx != len[x] + 1), len[y] - (dy != len[y] + 1)) + 1,
      ans = 0;
  while (l <= r) {
    int mid = l + r >> 1;
    if (hsh(x, dx, mid, 0) == hsh(y, dy, mid, 0) &&
        hsh(x, dx, mid, 1) == hsh(y, dy, mid, 1))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return getc(x, dx, ans + 1) >= getc(y, dy, ans + 1);
}
int main() {
  p[0] = 29;
  mod[0] = 1e9 + 7;
  ip[0] = power(p[0], mod[0] - 2, 0);
  p[1] = 31;
  mod[1] = 1e9 + 9;
  ip[1] = power(p[1], mod[1] - 2, 1);
  pw[0][0] = pw[0][1] = 1;
  for (int i = 1; i < N; i++) {
    pw[i][0] = (long long)pw[i - 1][0] * p[0] % mod[0];
    pw[i][1] = (long long)pw[i - 1][1] * p[1] % mod[1];
  }
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    len[i] = strlen(s + 1);
    for (int j = 1; j <= len[i]; j++) t[i].push_back(s[j]);
    c[i][0].push_back(0);
    c[i][1].push_back(0);
    for (int j = 1; j <= len[i]; j++) {
      c[i][0].push_back(
          (c[i][0][j - 1] + (long long)pw[j - 1][0] * (s[j] - 'a' + 1)) %
          mod[0]);
      c[i][1].push_back(
          (c[i][1][j - 1] + (long long)pw[j - 1][1] * (s[j] - 'a' + 1)) %
          mod[1]);
    }
    nxt[len[i]] = len[i];
    for (int j = len[i] - 1; j; j--)
      if (s[j] != s[j + 1])
        nxt[j] = j + 1;
      else
        nxt[j] = nxt[j + 1];
    int l = 1, r = len[i];
    for (int j = 1; j <= len[i]; j++)
      if (s[nxt[j]] <= s[j])
        q[l++] = j;
      else
        q[r--] = j;
    for (int j = 1; j <= len[i]; j++) {
      a[i].push_back(q[j]);
      if (q[j] == len[i]) a[i].push_back(len[i] + 1);
    }
  }
  for (int i = 0; i <= len[1]; i++) f[1][i] = 1;
  for (int i = 2; i <= n; i++, now ^= 1) {
    int pre = 0, ans = 0;
    for (int j = 0; j <= len[i]; j++) {
      while (pre <= len[i - 1] && check(i, a[i][j], i - 1, a[i - 1][pre]))
        ans = (ans + f[now ^ 1][pre++]) % mans;
      f[now][j] = ans;
    }
  }
  for (int i = 0; i <= len[n]; i++) ans = (ans + f[now ^ 1][i]) % mans;
  printf("%d\n", ans);
}