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 long long p1 = 41, p2 = 43;
const long long mod1 = 1e9 + 9, mod2 = 1e9 + 7;
const long long MOD = 1e9 + 7;
long long ip1, ip2;
int n;
int sz[100003];
string S[100003];
vector<int> dp[100003];
vector<int> vec[100003];
vector<long long> pref1[100003];
long long pwr(long long a, long long b, long long mod) {
  if (b == 0)
    return 1;
  else if (b == 2)
    return (a * a) % mod;
  else if (b & 1)
    return (pwr(a, b - 1, mod) * a) % mod;
  else
    return pwr(pwr(a, b / 2, mod), 2, mod);
}
void print(vector<int> vec) {
  for (int x : vec) cout << x << " ";
  cout << "\n";
}
long long hash1(int idx, int a, int er) {
  if (er == -1)
    return pref1[idx][a];
  else if (a < er)
    return pref1[idx][a];
  else {
    long long temp = (pref1[idx][a + 1] - pref1[idx][er]) * ip1;
    temp = ((temp % mod1) + mod1) % mod1;
    if (er)
      return (pref1[idx][er - 1] + temp) % mod1;
    else
      return temp;
  }
}
void create_pref(int idx) {
  pref1[idx].assign(sz[idx], 0);
  long long tmp1 = 1;
  for (int i = 0; i < sz[idx]; i++) {
    pref1[idx][i] = tmp1 * (S[idx][i] - 'a' + 1);
    if (i) pref1[idx][i] += pref1[idx][i - 1];
    pref1[idx][i] %= mod1;
    tmp1 = (tmp1 * p1) % mod1;
  }
}
void create_vec(int idx, vector<int> &vec) {
  string s = S[idx];
  int kir = 0, kan = s.size();
  vec.assign(s.size() + 1, 0);
  int l = 0;
  for (int i = 0; i < s.size(); i++) {
    if (i + 1 == s.size() || s[i + 1] != s[i]) {
      if (i + 1 == s.size() || s[i + 1] < s[i]) {
        for (int j = l; j <= i; j++) vec[kir++] = j;
      } else {
        for (int j = l; j <= i; j++) vec[kan--] = j;
      }
      l = i + 1;
    }
  }
  vec[kir] = -1;
}
bool compare(int i, int e1, int j, int e2) {
  int leni = sz[i] - (e1 != -1);
  int lenj = sz[j] - (e2 != -1);
  int kir = 0, kan = min(leni, lenj);
  while (kir < kan) {
    int mid = (kir + kan) / 2;
    if (hash1(i, mid, e1) == hash1(j, mid, e2))
      kir = mid + 1;
    else
      kan = mid;
  }
  int a = kir + (kir >= e1 && e1 != -1);
  int b = kir + (kir >= e2 && e2 != -1);
  if (kir == leni)
    return 1;
  else if (kir == lenj)
    return 0;
  else
    return S[i][a] <= S[j][b];
}
int main() {
  ip1 = pwr(p1, mod1 - 2, mod1);
  ip2 = pwr(p2, mod2 - 2, mod2);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> S[i];
    sz[i] = S[i].size();
    dp[i].assign(S[i].size() + 3, 0);
  }
  for (int i = 1; i <= n; i++) create_vec(i, vec[i]);
  for (int i = 1; i <= n; i++) create_pref(i);
  for (int i = 0; i <= sz[1]; i++) dp[1][i]++;
  for (int i = 1; i + 1 <= n; i++) {
    int idx = 0;
    for (int k = 0; k <= sz[i]; k++) {
      while (idx <= sz[i + 1] && !compare(i, vec[i][k], i + 1, vec[i + 1][idx]))
        idx++;
      if (idx > sz[i + 1]) break;
      dp[i + 1][idx] = (dp[i + 1][idx] + dp[i][k]) % MOD;
    }
    for (int k = 1; k <= sz[i + 1]; k++)
      dp[i + 1][k] = (dp[i + 1][k] + dp[i + 1][k - 1]) % MOD;
  }
  int res = 0;
  for (int i = 0; i <= sz[n]; i++) res = (res + dp[n][i]) % MOD;
  cout << res << "\n";
}