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 M = 1e6 + 1;
const int N = 1e5 + 1;
const long long base = 37;
const long long mod = 1e9 + 7;
long long ff[M], h[2][M], f[2][M];
string str[N];
int nxt[2][M], sorted[2][M];
void get_sorted(int id) {
  for (int i = str[id].size() - 1; i >= 0; i--) {
    if (str[id][i] != str[id][i + 1])
      nxt[id % 2][i] = i + 1;
    else
      nxt[id % 2][i] = nxt[id % 2][i + 1];
  }
  h[id % 2][0] = str[id][0];
  for (int i = 1; i <= str[id].size(); i++)
    h[id % 2][i] = h[id % 2][i - 1] * base + str[id][i];
  int l = 0;
  int r = str[id].size() - 1;
  for (int i = 0; i < str[id].size(); i++) {
    if (str[id][i] > str[id][nxt[id % 2][i]]) {
      sorted[id % 2][l] = i;
      l++;
    } else {
      sorted[id % 2][r] = i;
      r--;
    }
  }
  for (int i = str[id].size(); i > 0; i--) {
    if (sorted[id % 2][i - 1] != str[id].size() - 1)
      sorted[id % 2][i] = sorted[id % 2][i - 1];
    else {
      sorted[id % 2][i] = str[id].size();
      break;
    }
  }
}
long long get_hash(int x, int cx, int pos) {
  if (pos < cx) {
    return h[x % 2][pos];
  }
  long long now = 0;
  if (cx > 0) now = h[x % 2][cx - 1];
  now *= ff[pos + 1 - cx];
  now += h[x % 2][pos + 1] - ff[pos + 1 - cx] * h[x % 2][cx];
  return now;
}
char get_c(int x, int cx, int pos) {
  if (pos < cx) return str[x][pos];
  if (str[x].size() > pos + 1) return str[x][pos + 1];
  return 'a' - 1;
}
int check(int x, int cx, int y, int cy) {
  int l = 0;
  int r = min(str[x].size(), str[y].size()) + 1;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (get_hash(x, cx, mid - 1) != get_hash(y, cy, mid - 1))
      r = mid;
    else
      l = mid;
  }
  if (get_c(x, cx, l) <= get_c(y, cy, l)) return 1;
  return 0;
}
int main() {
  ff[0] = 1;
  for (int i = 1; i <= 1e6; i++) ff[i] = ff[i - 1] * base;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) cin >> str[i];
  get_sorted(1);
  for (int i = 0; i <= str[1].size(); i++) f[1][i] = 1;
  for (int i = 2; i <= n; i++) {
    get_sorted(i);
    int k = 0;
    int x = sorted[(i - 1) % 2][k];
    long long sum = 0;
    for (int j = 0; j <= str[i].size(); j++) {
      int y = sorted[i % 2][j];
      while (k <= str[i - 1].size() && check(i - 1, x, i, y)) {
        sum += f[(i - 1) % 2][k];
        if (sum >= mod) sum -= mod;
        k++;
        x = sorted[(i - 1) % 2][k];
      }
      f[i % 2][j] = sum;
    }
  }
  long long ans = 0;
  for (int i = 0; i <= str[n].size(); i++) {
    ans += f[n % 2][i];
    if (ans >= mod) ans -= mod;
  }
  cout << ans;
  return 0;
}