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 MOD = 1e9 + 7;
const long long N = 1e6 + 10;
const long long M = 1e5 + 10;
const long long P = 239017;
void add(long long &a, long long b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}
long long pl[N], p_pow[N];
vector<long long> hs[N];
string s[M];
inline char get_letter(pair<long long, long long> a, long long num) {
  if (!a.second || a.second > num) {
    return s[a.first][num - 1];
  }
  return s[a.first][num];
}
inline long long get_hash(pair<long long, long long> a, long long len) {
  if (a.second == 0 || a.second > len) {
    return hs[a.first][len] * p_pow[1000000 - len];
  }
  return (hs[a.first][a.second - 1] * P +
          (hs[a.first][len + 1] - hs[a.first][a.second])) *
         p_pow[1000000 - len - 1];
}
string get_s(pair<long long, long long> a, long long len) {
  string res;
  for (long long i = 0; i < (long long)(s[a.first]).size(); i++) {
    if ((long long)(res).size() < len && i + 1 != a.second) {
      res.push_back(s[a.first][i]);
    }
  }
  return res;
}
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
  long long len1 = (long long)(s[a.first]).size() - (a.second != 0),
            len2 = (long long)(s[b.first]).size() - (b.second != 0);
  long long l = 0, r = min(len1, len2) + 1;
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (get_hash(a, mid) == get_hash(b, mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  if (l == min(len1, len2)) {
    if (len1 == len2) return a < b;
    return len1 < len2;
  }
  return get_letter(a, l + 1) < get_letter(b, l + 1);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  p_pow[0] = 1;
  for (long long i = 1; i <= 1e6; i++) {
    p_pow[i] = p_pow[i - 1] * P;
  }
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> s[i];
    hs[i].resize((long long)(s[i]).size() + 1);
    for (long long j = 1; j <= (long long)(s[i]).size(); j++) {
      hs[i][j] = hs[i][j - 1] + p_pow[j] * s[i][j - 1];
    }
  }
  vector<vector<long long>> order(n);
  for (long long i = 0; i < n; i++) {
    order[i].resize((long long)(s[i]).size() + 1);
    long long l = 0, r = (long long)(s[i]).size();
    vector<long long> nxt((long long)(s[i]).size());
    nxt[(long long)(s[i]).size() - 1] = 0;
    for (long long j = (long long)(s[i]).size() - 2; j >= 0; j--) {
      if (s[i][j] == s[i][j + 1]) {
        nxt[j] = nxt[j + 1] + 1;
      } else {
        nxt[j] = 0;
      }
    }
    for (long long j = 0; j < (long long)(s[i]).size(); j++) {
      long long tpos = j + nxt[j];
      if (tpos + 1 == (long long)(s[i]).size() || s[i][tpos + 1] < s[i][tpos]) {
        order[i][l++] = j + 1;
      } else {
        order[i][r--] = j + 1;
      }
    }
    order[i][l] = 0;
  }
  vector<vector<long long>> dp(n);
  dp[0].resize((long long)(s[0]).size() + 1);
  dp[0][0] = 1;
  for (long long i = 0; i + 1 < n; i++) {
    dp[i + 1].resize((long long)(s[i + 1]).size() + 1);
    long long ptr = (long long)(s[i + 1]).size() + 1;
    for (long long j = 1; j <= (long long)(s[i]).size(); j++) {
      add(dp[i][j], dp[i][j - 1]);
    }
    for (long long j = (long long)(s[i]).size(); j >= 0; j--) {
      while (ptr > 0 && cmp(make_pair(i, order[i][j]),
                            make_pair(i + 1, order[i + 1][ptr - 1]))) {
        ptr--;
      }
      if (ptr <= (long long)(s[i + 1]).size()) {
        add(dp[i + 1][ptr], dp[i][j]);
      }
    }
  }
  long long ans = 0;
  for (long long i = 0; i <= (long long)(s[n - 1]).size(); i++) {
    if (i > 0) {
      add(dp[n - 1][i], dp[n - 1][i - 1]);
    }
    add(ans, dp[n - 1][i]);
  }
  cout << ans << '\n';
}