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;
int n;
vector<string> a;
unordered_map<long long, long long> dp[100001];
long long mod = 1e9 + 7;
const int p = 31;
vector<vector<long long> > hsh;
vector<vector<int> > order;
vector<long long> ppow;
void pre_compute_hash() {
  ppow.resize(1e6 + 1);
  ppow[0] = 1;
  for (int i = 1; i < ppow.size(); i++) {
    ppow[i] = ppow[i - 1] * p;
  }
  hsh.resize(n);
  for (int i = 0; i < n; i++) {
    hsh[i].resize(a[i].size() + 1, 0);
    for (int j = 0; j < a[i].size(); j++) {
      hsh[i][j + 1] = hsh[i][j] + (a[i][j] - 'a') * ppow[j];
    }
  }
}
char getind(int i, int x, int j) {
  if (x == 0 || x - 1 > j) {
    return a[i][j];
  }
  return a[i][j + 1];
}
long long get_hash(int i, int x, int j) {
  j += 1;
  if (x == 0 || x > j) {
    return hsh[i][j] * p;
  }
  return hsh[i][x - 1] * p + (hsh[i][j + 1] - hsh[i][x]);
}
bool cmp(int i, int x, int j, int y) {
  int l = 0;
  int h1 = (int)a[i].size() - (x != 0);
  int h2 = (int)a[j].size() - (y != 0);
  int h = min(h1, h2) - 1;
  while (l <= h) {
    int m = l + (h - l) / 2;
    if (get_hash(i, x, m) == get_hash(j, y, m)) {
      l = m + 1;
    } else {
      h = m - 1;
    }
  }
  if (l >= min(h1, h2)) {
    return h1 <= h2;
  }
  return getind(i, x, l) <= getind(j, y, l);
}
void solve() {
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    a[i] = s;
  }
  pre_compute_hash();
  order.resize(n);
  for (int i = 0; i < n; i++) {
    order[i].resize(a[i].size() + 1);
    int l = 0;
    int r = a[i].size();
    vector<int> nxt(a[i].size(), a[i].size());
    for (int j = (int)a[i].size() - 2; j >= 0; j--) {
      if (a[i][j] != a[i][j + 1]) {
        nxt[j] = j + 1;
      } else {
        nxt[j] = nxt[j + 1];
      }
    }
    for (int j = 0; j < a[i].size(); j++) {
      if (nxt[j] == a[i].size() || a[i][nxt[j]] < a[i][j]) {
        order[i][l++] = j + 1;
      } else {
        order[i][r--] = j + 1;
      }
    }
    order[i][l] = 0;
    reverse(order[i].begin(), order[i].end());
  }
  for (int i = 0; i < (int)a.back().size() + 1; i++) {
    dp[n - 1][i] = 1;
  }
  for (int i = n - 2; i >= 0; i--) {
    int l = 0;
    long long cur = 0;
    for (int j = 0; j < order[i].size(); j++) {
      while (l < order[i + 1].size() &&
             cmp(i, order[i][j], i + 1, order[i + 1][l])) {
        cur += dp[i + 1][l];
        cur %= mod;
        l++;
      }
      dp[i][j] = cur;
    }
  }
  long long ans = 0;
  for (int i = 0; i < order[0].size(); i++) {
    ans += dp[0][i];
    ans %= mod;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}