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>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
template <const int &MOD>
struct _m_int {
  int val;
  _m_int(int64_t v = 0) {
    if (v < 0) v = v % MOD + MOD;
    if (v >= MOD) v %= MOD;
    val = int(v);
  }
  _m_int(uint64_t v) {
    if (v >= MOD) v %= MOD;
    val = int(v);
  }
  _m_int(int v) : _m_int(int64_t(v)) {}
  _m_int(unsigned v) : _m_int(uint64_t(v)) {}
  static int inv_mod(int a, int m = MOD) {
    int g = m, r = a, x = 0, y = 1;
    while (r != 0) {
      int q = g / r;
      g %= r;
      swap(g, r);
      x -= q * y;
      swap(x, y);
    }
    return x < 0 ? x + m : x;
  }
  explicit operator int() const { return val; }
  explicit operator unsigned() const { return val; }
  explicit operator int64_t() const { return val; }
  explicit operator uint64_t() const { return val; }
  explicit operator double() const { return val; }
  explicit operator long double() const { return val; }
  _m_int &operator+=(const _m_int &other) {
    val -= MOD - other.val;
    if (val < 0) val += MOD;
    return *this;
  }
  _m_int &operator-=(const _m_int &other) {
    val -= other.val;
    if (val < 0) val += MOD;
    return *this;
  }
  static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
    return unsigned(x % m);
    unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
    unsigned quot, rem;
    asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
    return rem;
  }
  _m_int &operator*=(const _m_int &other) {
    val = fast_mod(uint64_t(val) * other.val);
    return *this;
  }
  _m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
  friend _m_int operator+(const _m_int &a, const _m_int &b) {
    return _m_int(a) += b;
  }
  friend _m_int operator-(const _m_int &a, const _m_int &b) {
    return _m_int(a) -= b;
  }
  friend _m_int operator*(const _m_int &a, const _m_int &b) {
    return _m_int(a) *= b;
  }
  friend _m_int operator/(const _m_int &a, const _m_int &b) {
    return _m_int(a) /= b;
  }
  _m_int &operator++() {
    val = val == MOD - 1 ? 0 : val + 1;
    return *this;
  }
  _m_int &operator--() {
    val = val == 0 ? MOD - 1 : val - 1;
    return *this;
  }
  _m_int operator++(int) {
    _m_int before = *this;
    ++*this;
    return before;
  }
  _m_int operator--(int) {
    _m_int before = *this;
    --*this;
    return before;
  }
  _m_int operator-() const { return val == 0 ? 0 : MOD - val; }
  friend bool operator==(const _m_int &a, const _m_int &b) {
    return a.val == b.val;
  }
  friend bool operator!=(const _m_int &a, const _m_int &b) {
    return a.val != b.val;
  }
  friend bool operator<(const _m_int &a, const _m_int &b) {
    return a.val < b.val;
  }
  friend bool operator>(const _m_int &a, const _m_int &b) {
    return a.val > b.val;
  }
  friend bool operator<=(const _m_int &a, const _m_int &b) {
    return a.val <= b.val;
  }
  friend bool operator>=(const _m_int &a, const _m_int &b) {
    return a.val >= b.val;
  }
  _m_int inv() const { return inv_mod(val); }
  _m_int pow(int64_t p) const {
    if (p < 0) return inv().pow(-p);
    _m_int a = *this, result = 1;
    while (p > 0) {
      if (p & 1) result *= a;
      p >>= 1;
      if (p > 0) a *= a;
    }
    return result;
  }
  friend ostream &operator<<(ostream &os, const _m_int &m) {
    return os << m.val;
  }
};
extern const int MOD = 1e9 + 7;
using mod_int = _m_int<MOD>;
namespace SuffixArray {
const int N = 1e6 + 5;
int sa[N], rk[N], ht[N];
bool t[N << 1];
bool islms(const int i, const bool *t) { return i > 0 && t[i] && !t[i - 1]; }
template <class T>
inline void sort(T s, int *sa, const int len, const int sz, const int sigma,
                 bool *t, int *b, int *cb, int *p) {
  memset(b, 0, sizeof(int) * sigma);
  memset(sa, -1, sizeof(int) * len);
  for (int i = 0; i < int(len); ++i) b[(int)s[i]]++;
  cb[0] = b[0];
  for (int i = 1; i < int(sigma); ++i) cb[i] = cb[i - 1] + b[i];
  for (int i = (sz)-1; i >= (0); --i) sa[--cb[(int)s[p[i]]]] = p[i];
  for (int i = 1; i < int(sigma); ++i) cb[i] = cb[i - 1] + b[i - 1];
  for (int i = 0; i < int(len); ++i)
    if (sa[i] > 0 && !t[sa[i] - 1]) sa[cb[(int)s[sa[i] - 1]]++] = sa[i] - 1;
  cb[0] = b[0];
  for (int i = 1; i < int(sigma); ++i) cb[i] = cb[i - 1] + b[i];
  for (int i = (len)-1; i >= (0); --i)
    if (sa[i] > 0 && t[sa[i] - 1]) sa[--cb[(int)s[sa[i] - 1]]] = sa[i] - 1;
}
template <class T>
inline void sais(T s, int *sa, const int len, bool *t, int *b, int *b1,
                 const int sigma) {
  int p = -1, *cb = b + sigma;
  t[len - 1] = 1;
  for (int i = (len - 1) - 1; i >= (0); --i)
    t[i] = s[i] < s[i + 1] || (s[i] == s[i + 1] && t[i + 1]);
  int sz = 0, cnt = 0;
  for (int i = 1; i < int(len); ++i)
    if (t[i] && !t[i - 1]) b1[sz++] = i;
  sort(s, sa, len, sz, sigma, t, b, cb, b1);
  sz = 0;
  for (int i = 0; i < int(len); ++i)
    if (islms(sa[i], t)) sa[sz++] = sa[i];
  for (int i = sz; i < int(len); ++i) sa[i] = -1;
  for (int i = 0; i < int(sz); ++i) {
    int x = sa[i];
    for (int j = 0; j < int(len); ++j) {
      if (p == -1 || s[x + j] != s[p + j] || t[x + j] != t[p + j]) {
        cnt++;
        p = x;
        break;
      } else if (j > 0 && (islms(x + j, t) || islms(p + j, t))) {
        break;
      }
    }
    sa[sz + (x >>= 1)] = cnt - 1;
  }
  for (int i = len - 1, j = len - 1; i >= sz; i--)
    if (sa[i] >= 0) sa[j--] = sa[i];
  int *s1 = sa + len - sz, *b2 = b1 + sz;
  if (cnt < sz)
    sais(s1, sa, sz, t + len, b, b1 + sz, cnt);
  else
    for (int i = 0; i < int(sz); ++i) sa[s1[i]] = i;
  for (int i = 0; i < int(sz); ++i) b2[i] = b1[sa[i]];
  sort(s, sa, len, sz, sigma, t, b, cb, b2);
}
template <class T>
inline void getHeight(T s, int n) {
  for (int i = 1; i < int(n + 1); ++i) rk[sa[i]] = i;
  int j = 0, k = 0;
  for (int i = 0; i < n; ht[rk[i++]] = k)
    for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; k++)
      ;
}
template <class T>
inline void init(T s, const int len, const int sigma) {
  sais(s, sa, len, t, rk, ht, sigma);
}
template <class T>
inline void solve(T s, int len) {
  init(s, len + 1, 128);
}
}  // namespace SuffixArray
template <typename T, bool maximum_mode = false>
struct RMQ {
  int n = 0;
  vector<vector<T>> range_min;
  RMQ(const vector<T> &values = {}) {
    if (!values.empty()) build(values);
  }
  static int largest_bit(int x) { return 31 - __builtin_clz(x); }
  static T better(T a, T b) { return maximum_mode ? max(a, b) : min(a, b); }
  void build(const vector<T> &values) {
    n = int(values.size());
    int levels = largest_bit(n) + 1;
    range_min.resize(levels);
    for (int k = 0; k < levels; k++) range_min[k].resize(n - (1 << k) + 1);
    if (n > 0) range_min[0] = values;
    for (int k = 1; k < levels; k++)
      for (int i = 0; i <= n - (1 << k); i++)
        range_min[k][i] =
            better(range_min[k - 1][i], range_min[k - 1][i + (1 << (k - 1))]);
  }
  T query_value(int a, int b) const {
    assert(0 <= a && a < b && b <= n);
    int level = largest_bit(b - a);
    return better(range_min[level][a], range_min[level][b - (1 << level)]);
  }
};
template <typename T_string = string>
struct suffix_array {
  int n = 0;
  T_string str;
  vector<int> suffix;
  vector<int> rank;
  vector<int> lcp;
  RMQ<int> rmq;
  suffix_array() {}
  suffix_array(const T_string &_str, bool build_rmq = true) {
    build(_str, build_rmq);
  }
  void build(const T_string &_str, bool build_rmq = true) {
    str = _str;
    n = int(str.size());
    SuffixArray::solve(str, n);
    suffix.resize(n);
    for (int i = 0; i < n; i++) suffix[i] = SuffixArray::sa[i + 1];
    rank.resize(n);
    for (int i = 0; i < n; i++) rank[suffix[i]] = i;
    compute_lcp();
    if (build_rmq) rmq.build(lcp);
  }
  void compute_lcp() {
    lcp.assign(n, 0);
    int match = 0;
    for (int i = 0; i < n; i++) {
      if (rank[i] == 0) continue;
      int a = suffix[rank[i]] + match;
      int b = suffix[rank[i] - 1] + match;
      while (a < n && b < n && str[a++] == str[b++]) match++;
      lcp[rank[i]] = match;
      match = max(match - 1, 0);
    }
  }
  int get_lcp_from_ranks(int a, int b) const {
    if (a == b) return n - suffix[a];
    if (a > b) swap(a, b);
    return rmq.query_value(a + 1, b + 1);
  }
  int get_lcp(int a, int b) const {
    if (a >= n || b >= n) return 0;
    if (a == b) return n - a;
    return get_lcp_from_ranks(rank[a], rank[b]);
  }
  int compare(int a, int b, int length = -1) const {
    if (length < 0) length = n;
    if (a == b) return 0;
    int common = get_lcp(a, b);
    if (common >= length) return 0;
    if (a + common >= n || b + common >= n) return a + common >= n ? -1 : 1;
    return str[a + common] < str[b + common]
               ? -1
               : (str[a + common] == str[b + common] ? 0 : 1);
  }
};
const char SPECIAL1 = '#';
const char SPECIAL2 = '$';
vector<mod_int> solve(string word, string next_word, vector<mod_int> dp) {
  assert(SPECIAL1 < SPECIAL2);
  word += SPECIAL1;
  next_word += SPECIAL2;
  int n = int(word.size());
  int m = int(next_word.size());
  next_word += SPECIAL2;
  assert(int(dp.size()) == n);
  vector<mod_int> dp_prefix_sum(n + 1, 0);
  for (int i = 0; i < n; i++) dp_prefix_sum[i + 1] = dp_prefix_sum[i] + dp[i];
  auto &&get_dp_sum = [&](int start, int end) {
    assert(0 <= start && start <= end && end <= n);
    return dp_prefix_sum[end] - dp_prefix_sum[start];
  };
  vector<mod_int> next_dp_change(m + 1, 0);
  auto &&increment_next_dp = [&](int start, int end, mod_int amount) {
    assert(0 <= start && start <= end && end <= m);
    next_dp_change[start] += amount;
    next_dp_change[end] -= amount;
  };
  suffix_array<string> SA(word + next_word);
  auto &&get_lcp = [&](int a, int b) {
    assert(0 <= a && a <= n);
    assert(0 <= b && b <= m);
    return min({SA.get_lcp(a, n + b), n - a, m - b});
  };
  auto &&less_equal = [&](int a, int b) {
    assert(0 <= a && a <= n);
    assert(0 <= b && b <= m);
    return a == n || SA.compare(a, n + b) <= 0;
  };
  vector<mod_int> less_equal_dp(min(n, m));
  for (int i = 0; i < min(n, m); i++)
    if (less_equal(i + 1, i + 1)) less_equal_dp[i] = dp[i];
  vector<mod_int> less_equal_prefix_sum(min(n, m) + 1, 0);
  for (int i = 0; i < min(n, m); i++)
    less_equal_prefix_sum[i + 1] = less_equal_prefix_sum[i] + less_equal_dp[i];
  auto &&get_less_equal_sum = [&](int start, int end) {
    assert(0 <= start && start <= end && end <= min(n, m));
    return less_equal_prefix_sum[end] - less_equal_prefix_sum[start];
  };
  vector<mod_int> less_equal_change(m + 1, 0);
  auto &&increment_less_equal = [&](int start, int end, mod_int amount) {
    assert(0 <= start && start <= end && end <= min(n, m));
    less_equal_change[start] += amount;
    less_equal_change[end] -= amount;
  };
  vector<mod_int> next_dp(m, 0);
  int status = 0;
  for (int i = 0; i < min(n, m); i++) {
    if (status > 0) break;
    if (status < 0) {
      next_dp[i] += get_dp_sum(i, n);
      increment_next_dp(i + 1, m, dp[i]);
      continue;
    }
    assert(status == 0);
    if (less_equal(i + 1, i + 1)) next_dp[i] += dp[i];
    int lcp = get_lcp(i, i + 1);
    next_dp[i] += get_less_equal_sum(i + 1, i + lcp + 1);
    if (less_equal(i, i + 1)) next_dp[i] += get_dp_sum(i + lcp + 1, n);
    lcp = get_lcp(i + 1, i);
    increment_less_equal(i + 1, i + lcp + 1, dp[i]);
    if (less_equal(i + 1, i)) increment_next_dp(i + lcp + 1, m, dp[i]);
    status = word[i] - next_word[i];
  }
  mod_int change = 0, le_change = 0;
  for (int i = 0; i < m; i++) {
    change += next_dp_change[i];
    le_change += less_equal_change[i];
    next_dp[i] += change;
    if (i >= n || less_equal(i + 1, i + 1)) next_dp[i] += le_change;
  }
  return next_dp;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  string word;
  cin >> N >> word;
  vector<mod_int> dp(word.size() + 1, 1);
  for (int i = 1; i < N; i++) {
    string next_word;
    cin >> next_word;
    dp = solve(word, next_word, dp);
    word = next_word;
    ;
  }
  cout << accumulate(dp.begin(), dp.end(), mod_int(0)) << '\n';
}