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 p = 1e9 + 7, m = 131;
int n;
string ss[1000086];
char s[1000086];
vector<unsigned long long> v[1000086];
unsigned long long sum[1000086];
inline unsigned long long geth(int i, int len, int x) {
  if (x > len) return v[i][len];
  unsigned long long lsum = v[i][x - 1],
                     rsum = v[i][len + 1] - v[i][x] * sum[len + 1 - x];
  return lsum * sum[len + 1 - x] + rsum;
}
inline char getc(int i, int len, int x) {
  int j = x > len ? (len - 1) : len;
  if (j > ss[i].length()) return 0;
  return ss[i][j];
}
inline bool leq(int i, int x, int j, int y) {
  int lenx = x <= ss[i].length() ? (ss[i].length() - 1) : ss[i].length();
  int leny = y <= ss[j].length() ? (ss[j].length() - 1) : ss[j].length();
  int l = 0, r = min(lenx, leny), mid;
  while (l < r) {
    mid = (l + r >> 1) + 1;
    if (geth(i, mid, x) == geth(j, mid, y))
      l = mid;
    else
      r = mid - 1;
  }
  return getc(i, l + 1, x) <= getc(j, l + 1, y);
}
char nxt[1000086];
int sa[1000086];
vector<int> a[1000086];
int f[2][1000086];
int main() {
  sum[0] = 1;
  for (int i = 1; i < 1000086; i++) sum[i] = sum[i - 1] * m;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    ss[i] = s + 1;
    int len = strlen(s + 1);
    v[i].push_back(0);
    for (int j = 1; j <= len; j++) v[i].push_back(v[i].back() * m + s[j]);
    for (int j = len; j; j--) {
      if (s[j + 1] != s[j])
        nxt[j] = s[j + 1];
      else
        nxt[j] = nxt[j + 1];
    }
    int l = 1, r = len;
    for (int j = 1; j <= len; j++) {
      if (nxt[j] > s[j])
        sa[r--] = j;
      else
        sa[l++] = j;
    }
    for (int j = 1; j <= len; j++) {
      a[i].push_back(sa[j]);
      if (sa[j] == len) a[i].push_back(len + 1);
    }
  }
  for (int i = 0; i < a[1].size(); i++) f[1][i] = 1;
  for (int i = 2; i <= n; i++) {
    int x = -1, sum = 0;
    for (int j = 0; j < a[i].size(); j++) {
      while (x + 1 < a[i - 1].size() &&
             leq(i - 1, a[i - 1][x + 1], i, a[i][j])) {
        sum += f[i & 1 ^ 1][++x];
        if (sum >= p) sum -= p;
      }
      f[i & 1][j] = sum;
    }
  }
  int ans = 0;
  for (int i = 0; i < a[n].size(); i++) {
    ans += f[n & 1][i];
    if (ans >= p) ans -= p;
  }
  printf("%d", ans);
}