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 = 1e9 + 7;
const int maxm = 1e6 + 1e5 + 10;
const long long p1 = 998246177;
template <typename T>
inline bool read(T &x) {
  x = 0;
  bool neg = 0;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-')
      neg = 1;
    else if (c == EOF)
      return 0;
  for (; '0' <= c && c <= '9'; c = getchar()) x = x * 10 + (c ^ 48);
  neg ? (x = -x) : 0;
  return c != EOF;
}
template <typename A, typename... B>
inline bool read(A &x, B &...y) {
  return read(x) && read(y...);
}
unsigned long long h[maxm];
unsigned long long P1[maxm];
int sum[maxm], flag[maxm];
char s[maxm];
int a[maxm], dp[maxm];
int sz;
vector<int> v[maxm / 10];
void update(int len) {
  h[++sz] = s[1] - 'a' + 1;
  for (int i = 2; i <= len; i++) {
    sz++;
    h[sz] = h[sz - 1] * p1 + s[i] - 'a' + 1;
  }
}
int getlcp(int i, int j, int k) {
  int ca1 = 0, ca2 = 0;
  if (j + sum[i - 1] != sum[i]) ca1++;
  if (k + sum[i - 2] != sum[i - 1]) ca2++;
  int l = 1,
      r = min(sum[i] - sum[i - 1] - 1 - ca1, sum[i - 1] - sum[i - 2] - 1 - ca2),
      res = 0;
  while (l <= r) {
    int mid = l + r >> 1;
    unsigned long long h1, h2;
    if (mid < j)
      h1 = h[sum[i - 1] + mid];
    else {
      if (j > 1)
        h1 = h[sum[i - 1] + j - 1];
      else
        h1 = 0;
      h1 = h1 * P1[mid - j + 1] + h[sum[i - 1] + mid + 1] -
           h[sum[i - 1] + j] * P1[mid + 1 - j];
    }
    if (mid < k)
      h2 = h[sum[i - 2] + mid];
    else {
      if (k > 1)
        h2 = h[sum[i - 2] + k - 1];
      else
        h2 = 0;
      h2 = h2 * P1[mid - k + 1] + h[sum[i - 2] + mid + 1] -
           h[sum[i - 2] + k] * P1[mid + 1 - k];
    }
    if (h1 == h2) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  return res;
}
int add(int a, int b) {
  a += b;
  if (a > mod) a %= mod;
  return a;
}
bool cmp(int i, int j) {
  if (i < j) return flag[i] == 0;
  return flag[j] == 1;
}
int main() {
  P1[0] = 1;
  int N = 1e6 + 1;
  for (int i = 1; i <= N; i++) P1[i] = P1[i - 1] * p1;
  int n;
  read(n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    s[len + 1] = i % 3 + 1;
    s[len + 2] = 0;
    update(len + 1);
    for (int j = 1; j <= len + 1; j++) a[sum[i - 1] + j] = s[j];
    sum[i] = sum[i - 1] + len + 1;
    for (int j = 1; j <= len + 1; j++) v[i].push_back(j);
    flag[len + 1] = 0;
    flag[len] = 0;
    for (int j = len - 1; j; j--) {
      if (s[j] == s[j + 1])
        flag[j] = flag[j + 1];
      else if (s[j] < s[j + 1])
        flag[j] = 1;
      else
        flag[j] = 0;
    }
    sort(v[i].begin(), v[i].end(), cmp);
  }
  for (int i = 1; i <= sum[1]; i++) dp[i] = 1;
  for (int i = 2; i <= n; i++) {
    int pt = -1, tot = 0;
    for (int z = 0; z < sum[i] - sum[i - 1]; z++) {
      int j = v[i][z] + sum[i - 1];
      for (int t = pt + 1; t < sum[i - 1] - sum[i - 2]; t++) {
        int k = v[i - 1][t] + sum[i - 2];
        int res = getlcp(i, j - sum[i - 1], k - sum[i - 2]);
        int ch1, ch2;
        if (res < j - sum[i - 1] - 1) {
          ch1 = a[sum[i - 1] + res + 1];
          if (sum[i - 1] + res + 1 > sum[i]) ch1 = 5;
        } else {
          ch1 = a[sum[i - 1] + res + 2];
          if (sum[i - 1] + res + 2 > sum[i]) ch1 = 5;
        }
        if (res < k - sum[i - 2] - 1) {
          ch2 = a[sum[i - 2] + res + 1];
          if (sum[i - 2] + res + 1 > sum[i - 1]) ch2 = -1;
        } else {
          ch2 = a[sum[i - 2] + res + 2];
          if (sum[i - 2] + res + 2 > sum[i - 1]) ch2 = -1;
        }
        if (ch2 <= 20) ch2 = -1;
        if (ch2 <= ch1) {
          pt = t;
          tot = (tot + dp[k]) % mod;
        } else
          break;
      }
      dp[j] = (dp[j] + tot) % mod;
    }
  }
  int ans = 0;
  for (int i = sum[n - 1] + 1; i <= sum[n]; i++) ans = add(ans, dp[i]);
  printf("%d\n", ans);
  return 0;
}