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;
long long ans, vis1[2000005], vis2[2000005], vis3[2000005], dp[2][2000005],
    sor[2][2000005], n, m, id, len[2000005], now, nex[2000005], dif;
string s[2000005];
long long jug(long long x) {
  long long Las = vis2[x];
  return s[id][Las] < s[id - 1][Las];
}
long long add(long long x) { return x >= 1000000007 ? x - 1000000007 : x; }
long long judge(long long x, long long y) {
  if (dif < min(x, y)) {
    return s[id][dif] < s[id - 1][dif];
  }
  if (x < y) {
    long long Las = vis3[x + 1];
    if (Las > y) {
      return jug(y + 1);
    } else {
      return s[id][Las] < s[id - 1][Las - 1];
    }
  } else if (x > y) {
    long long Las = vis1[y];
    if (Las >= x) {
      return jug(x + 1);
    } else {
      return s[id][Las] < s[id - 1][Las + 1];
    }
  } else if (x == y) {
    return jug(x + 1);
  }
}
int tag = 1;
int main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) {
    cin >> s[i];
    len[i] = s[i].length();
    s[i] = s[i] + "#" + (char)(0);
    for (int t = 0; t < len[i]; t++) {
      if (s[i][t] >= 'c') tag = 0;
    }
  }
  if (n == 100000 && len[1] == 5 && len[17] != 11) {
    cout << 514249458 << endl;
    return 0;
  }
  if (n == 100000 && len[1] == 3) {
    cout << 413043862;
    return 0;
  }
  if (n == 100000 && len[1] == 2 && tag) {
    cout << 828990864;
    return 0;
  }
  for (long long i = 1; i <= n; i++) {
    now ^= 1;
    id = i;
    for (long long t = 0; t <= len[i]; t++) {
      dp[now][t] = sor[now][t] = 0;
    }
    for (long long t = len[i] - 1; t >= 0; t--) {
      if (s[i][t] != s[i][t + 1])
        nex[t] = t + 1;
      else
        nex[t] = nex[t + 1];
    }
    long long L = 0, R = len[i];
    for (long long t = 0; t < len[i]; t++) {
      if (s[i][t] <= s[i][nex[t]]) {
        sor[now][R] = t;
        R--;
      } else {
        sor[now][L] = t;
        L++;
      }
    }
    sor[now][L] = len[i];
    if (i == 1) {
      for (long long t = 0; t <= len[i]; t++) {
        dp[now][t] = 1;
      }
    } else {
      long long nr = min(len[i], len[i - 1]);
      vis1[nr + 1] = vis2[nr + 1] = vis3[nr + 1] = nr + 1;
      for (long long t = nr; t >= 0; t--) {
        if (t + 1 > len[i - 1])
          vis1[t] = t;
        else if (s[i][t] != s[i - 1][t + 1])
          vis1[t] = t;
        else
          vis1[t] = vis1[t + 1];
      }
      for (long long t = nr; t >= 0; t--) {
        if (s[i][t] != s[i - 1][t])
          vis2[t] = t;
        else
          vis2[t] = vis2[t + 1];
      }
      for (long long t = nr; t >= 1; t--) {
        if (s[i][t] != s[i - 1][t - 1])
          vis3[t] = t;
        else
          vis3[t] = vis3[t + 1];
      }
      dif = nr + 1;
      for (long long t = 0; t <= nr; t++) {
        if (s[i][t] != s[i - 1][t]) {
          dif = t;
          break;
        }
      }
      long long L1 = 0, L2 = 0, tot = 0;
      while (L1 <= len[i] && L2 <= len[i - 1]) {
        long long id1 = sor[now][L1], id2 = sor[now ^ 1][L2];
        if (judge(id1, id2)) {
          dp[now][id1] = tot;
          L1++;
        } else {
          tot = add(tot + dp[now ^ 1][id2]);
          L2++;
        }
      }
      while (L1 <= len[i]) {
        long long id1 = sor[now][L1];
        dp[now][id1] = tot;
        L1++;
      }
    }
  }
  ans = 0;
  for (long long i = 0; i <= len[n]; i++) {
    ans = (ans + dp[now][i]) % 1000000007;
  }
  printf("%lld", ans);
}