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;
using VT = string;
struct RollingHash {
  static const signed long long mo0 = 1000000021, mo1 = 1000000009;
  static signed long long mul0, mul1;
  static const signed long long add0 = 1000010007, add1 = 1003333331;
  static vector<signed long long> pmo[2];
  VT s;
  int l;
  vector<signed long long> hash_[2];
  void init(VT s) {
    this->s = s;
    l = s.size();
    int i, j;
    hash_[0] = hash_[1] = vector<signed long long>(1, 0);
    if (!mul0)
      mul0 = 10009 + (((signed long long)&mul0 + time(NULL)) >> 5) % 259,
      mul1 = 10007 + (((signed long long)&mul1) >> 5) % 257;
    if (pmo[0].empty()) pmo[0].push_back(1), pmo[1].push_back(1);
    for (i = 0; i < (l); i++)
      hash_[0].push_back((hash_[0].back() * mul0 + add0 + s[i]) % mo0);
  }
  pair<signed long long, signed long long> hash(int l, int r) {
    if (l > r) return make_pair(0, 0);
    while (pmo[0].size() < r + 2) pmo[0].push_back(pmo[0].back() * mul0 % mo0);
    return make_pair(
        (hash_[0][r + 1] + (mo0 - hash_[0][l] * pmo[0][r + 1 - l] % mo0)) % mo0,
        0);
    return make_pair(
        (hash_[0][r + 1] + (mo0 - hash_[0][l] * pmo[0][r + 1 - l] % mo0)) % mo0,
        (hash_[1][r + 1] + (mo1 - hash_[1][l] * pmo[1][r + 1 - l] % mo1)) %
            mo1);
  }
  static pair<signed long long, signed long long> concat(
      pair<signed long long, signed long long> L,
      pair<signed long long, signed long long> R, int RL) {
    while (pmo[0].size() < RL + 2)
      pmo[0].push_back(pmo[0].back() * mul0 % mo0), pmo[1].push_back(0);
    return make_pair((R.first + L.first * pmo[0][RL]) % mo0, 0);
  }
};
vector<signed long long> RollingHash::pmo[2];
signed long long RollingHash::mul0, RollingHash::mul1;
int N;
string S[101010];
deque<int> Q[101010];
vector<signed long long> dp[101010];
RollingHash rh[101010];
const signed long long mo = 1000000007;
pair<signed long long, signed long long> gethash(int i, int p, int len) {
  if (len <= p) {
    return rh[i].hash(0, len - 1);
  }
  pair<signed long long, signed long long> L = rh[i].hash(0, p - 1);
  pair<signed long long, signed long long> R = rh[i].hash(p + 1, len);
  return rh[i].concat(L, R, len - p);
}
int comp(int i, int p, int j, int q) {
  int a = (int)S[i].size() - (p < S[i].size());
  int b = (int)S[j].size() - (q < S[j].size());
  int ml = min(a, b);
  int cur = 0;
  int x;
  for (x = 20; x >= 0; x--)
    if (cur + (1 << x) <= ml) {
      auto L = gethash(i, p, cur + (1 << x));
      auto R = gethash(j, q, cur + (1 << x));
      if (L == R) cur += 1 << x;
    }
  if (cur == ml) {
    return a <= b;
  } else {
    char c = S[i][cur + (cur >= p)];
    char d = S[j][cur + (cur >= q)];
    return c <= d;
  }
}
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> N;
  Q[0].push_back(0);
  dp[0].push_back(1);
  rh[0].init(S[0]);
  for (i = 0; i < (N); i++) {
    cin >> S[i + 1];
    rh[i + 1].init(S[i + 1]);
    dp[i + 1].resize(S[i + 1].size() + 1);
    vector<vector<int>> cand;
    j = 0;
    while (j < S[i + 1].size()) {
      cand.push_back({0, j, 1});
      x = j + 1;
      while (x < S[i + 1].size() && S[i + 1][j] == S[i + 1][x])
        x++, cand.back()[2]++;
      if (x == S[i + 1].size() || S[i + 1][j] > S[i + 1][x]) cand.back()[0] = 1;
      j = x;
    }
    reverse((cand.begin()), (cand.end()));
    Q[i + 1].push_back(S[i + 1].size());
    for (auto& c : cand) {
      if (c[0]) {
        for (j = 0; j < (c[2]); j++) Q[i + 1].push_front(c[1] + j);
      } else {
        for (j = 0; j < (c[2]); j++) Q[i + 1].push_back(c[1] + j);
      }
    }
    int p = 0, q;
    signed long long sum = 0;
    for (q = 0; q < (Q[i + 1].size()); q++) {
      while (p < Q[i].size() && comp(i, Q[i][p], i + 1, Q[i + 1][q]))
        (sum += dp[i][p++]) %= mo;
      dp[i + 1][q] = sum;
    }
  }
  signed long long sum = 0;
  for (auto& d : dp[N]) sum += d;
  cout << sum % mo << endl;
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}