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;
template <typename T>
vector<T>& operator--(vector<T>& v) {
  for (auto& i : v) --i;
  return v;
}
template <typename T>
vector<T>& operator++(vector<T>& v) {
  for (auto& i : v) ++i;
  return v;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& i : v) is >> i;
  return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
  for (auto& i : v) os << i << ' ';
  return os;
}
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
  os << p.first << ' ' << p.second;
  return os;
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
  return make_pair(a.first - b.first, a.second - b.second);
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
template <typename T, typename U>
void umin(T& a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
void umax(T& a, U b) {
  if (a < b) a = b;
}
const long long mod = 1e9 + 7;
int charcomp(string& s1, string& s2, int i1, int i2) {
  if (i1 == s1.size() && i2 == s2.size()) return 0;
  if (i1 == s1.size()) return -1;
  if (i2 == s2.size()) return 1;
  if (s1[i1] < s2[i2]) return -1;
  return 1;
}
int comp(string& s1, string& s2, int del1, int del2, vector<int>& same,
         vector<int>& samel, vector<int>& samer) {
  if (del1 > del2) return -comp(s2, s1, del2, del1, same, samer, samel);
  if (del1 == -1) {
    if (del2 == -1) {
      return charcomp(s1, s2, same[0], same[0]);
    }
    if (same[0] < del2) {
      return charcomp(s1, s2, same[0], same[0]);
    }
    int ln = samer[del2];
    return charcomp(s1, s2, del2 + ln + 0, del2 + ln + 1);
  }
  if (same[0] < min(del1, del2)) {
    return charcomp(s1, s2, same[0], same[0]);
  }
  int ln = samel[del1];
  if (ln >= del2 - del1) {
    ln = same[del2 + 1];
    return charcomp(s1, s2, del2 + 1 + ln, del2 + 1 + ln);
  }
  return charcomp(s1, s2, del1 + ln + 1, del1 + ln + 0);
}
int comp_easy(string& s1, string& s2, int del1, int del2) {
  int i1 = 0, i2 = 0;
  if (i1 == del1) ++i1;
  if (i2 == del2) ++i2;
  while (i1 < s1.size() && i2 < s2.size()) {
    if (s1[i1] != s2[i2]) {
      return (s1[i1] < s2[i2] ? -1 : 1);
    }
    ++i1;
    ++i2;
    if (i1 == del1) ++i1;
    if (i2 == del2) ++i2;
  }
  if (i1 == s1.size() && i2 == s2.size()) return 0;
  if (i1 == s1.size()) return -1;
  if (i2 == s2.size()) return 1;
  assert(false);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<string> v(n);
  cin >> v;
  vector<vector<int>> ord(n);
  for (int i = 0; i < n; ++i) {
    string s = v[i];
    int m = s.size();
    vector<int> tor(m, 1);
    for (int j = m - 2; j >= 0; --j) {
      if (s[j] == s[j + 1])
        tor[j] = tor[j + 1] + 1;
      else
        tor[j] = 1;
    }
    for (int j = 0; j < m; ++j) {
      ord[i].push_back(j);
    }
    ord[i].push_back(-1);
    function<int(int, int)> cmp = [&](int x, int y) {
      if (x == y) return 0;
      if (x == -1) {
        if (tor[y] >= m - y) return 1;
        char cy = s[y + tor[y]];
        char cx = s[y];
        if (cx < cy)
          return -1;
        else
          return 1;
      } else if (y == -1) {
        return -cmp(y, x);
      }
      if (x < y) {
        if (tor[x] >= y - x + 1) return 0;
        char cx = s[x + tor[x]];
        char cy = s[x];
        if (cx < cy)
          return -1;
        else
          return 1;
      } else {
        return -cmp(y, x);
      }
    };
    sort(ord[i].begin(), ord[i].end(),
         [&](int x, int y) { return cmp(x, y) < 0; });
  }
  vector<vector<pair<int, long long>>> res(n);
  for (int i = 0; i < ord[0].size(); ++i) {
    res[0].emplace_back(ord[0][i], 1);
  }
  for (int i = 1; i < n; ++i) {
    int ind = 0;
    long long sm = 0;
    int m = max(v[i].size(), v[i - 1].size()) + 5;
    int mn = min(v[i].size(), v[i - 1].size()) + 2;
    vector<int> same(m, 0), samel(m, 0), samer(m, 0);
    for (int j = mn - 1; j >= 0; --j) {
      if (j >= v[i].size() || j >= v[i - 1].size()) continue;
      if (v[i][j] == v[i - 1][j]) {
        same[j] = same[j + 1] + 1;
      } else {
        same[j] = 0;
      }
    }
    for (int j = mn - 1; j >= 0; --j) {
      if (j >= v[i].size() || j + 1 >= v[i - 1].size()) continue;
      if (v[i][j] == v[i - 1][j + 1]) {
        samel[j] = samel[j + 1] + 1;
      } else {
        samel[j] = 0;
      }
    }
    for (int j = mn - 1; j >= 0; --j) {
      if (j + 1 >= v[i].size() || j >= v[i - 1].size()) continue;
      if (v[i][j + 1] == v[i - 1][j]) {
        samer[j] = samer[j + 1] + 1;
      } else {
        samer[j] = 0;
      }
    }
    for (int j = 0; j < ord[i].size(); ++j) {
      res[i].emplace_back(ord[i][j], 0);
      while (ind < res[i - 1].size()) {
        int ans = comp(v[i - 1], v[i], res[i - 1][ind].first, res[i][j].first,
                       same, samel, samer);
        if (ans <= 0) {
          sm += res[i - 1][ind].second;
          ++ind;
          continue;
        } else {
          break;
        }
      }
      res[i].back().second = sm % mod;
    }
  }
  long long ans = 0;
  for (auto [a, b] : res.back()) {
    ans += b;
  }
  cout << ans % mod << '\n';
  return 0;
}