#include <bits/stdc++.h>
using namespace std;
struct num {
  static const int MA = 1e9 + 7, MB = 1e9 + 9;
  int a, b;
  num() {}
  num(int x) : a(x), b(x) {}
  num(int a, int b) : a(a), b(b) {}
  num operator+(const num& x) const {
    return num((a + x.a) % MA, (b + x.b) % MB);
  }
  num operator-(const num& x) const {
    return num((a + MA - x.a) % MA, (b + MB - x.b) % MB);
  }
  num operator*(int x) const {
    return num(((long long)a * x) % MA, ((long long)b * x) % MB);
  }
  num operator*(const num& x) const {
    return num(((long long)a * x.a) % MA, ((long long)b * x.b) % MB);
  }
  bool operator==(const num& x) const { return a == x.a && b == x.b; }
};
struct num2 {
  static const int MB = 1e9 + 9;
  int b;
  num2() {}
  num2(int x) : b(x) {}
  num2 operator+(const num2& x) const { return num2((b + x.b) % MB); }
  num2 operator-(const num2& x) const { return num2((b + MB - x.b) % MB); }
  num2 operator*(int x) const { return num2(((long long)b * x) % MB); }
  num2 operator*(const num2& x) const {
    return num2(((long long)b * x.b) % MB);
  }
  bool operator==(const num2& x) const { return b == x.b; }
};
const int MODP = 701;
template <class T>
struct StringOps {
  StringOps() {}
  StringOps(const string& s) : s(s) {
    n = s.length();
    deg.resize(n + 1);
    hashes.resize(n);
    deg[0] = T(1);
    for (int i = 1; i <= n; ++i) {
      deg[i] = deg[i - 1] * T(MODP);
    }
    T curhash = T(0);
    for (int i = 0; i < n; ++i) {
      curhash = curhash * T(MODP) + T(s[i]);
      hashes[i] = curhash;
    }
  }
  char get(int idx) { return s[idx]; }
  T get_hash(int l, int r) {
    T cur(0);
    if (l > 0) {
      cur = hashes[l - 1];
    }
    return hashes[r] - cur * deg[r - l + 1];
  }
  int string_lcp(int l, int r, int lim = 1e9) {
    int cleft = 1, cright = min(n - r, n - l);
    cright = min(lim, cright);
    int res = 0;
    while (cleft <= cright) {
      int key = (cleft + cright) / 2;
      if (get_hash(l, l + key - 1) == get_hash(r, r + key - 1)) {
        res = key;
        cleft = key + 1;
      } else {
        cright = key - 1;
      }
    }
    return res;
  }
  bool substring_cmp(int l1, int r1, int l2, int r2) {
    int lcp = string_lcp(l1, l2);
    int len1 = r1 - l1 + 1, len2 = r2 - l2 + 1;
    if (lcp >= len1 && lcp >= len2) {
      return len1 < len2;
    }
    if (lcp >= len1) {
      return true;
    }
    if (lcp >= len2) {
      return false;
    }
    return s[l1 + lcp] < s[l2 + lcp];
  }
  vector<T> hashes;
  vector<T> deg;
  string s;
  int n;
};
const int maxN = 1000100;
const int MOD = 1e9 + 7;
int len1, len2;
int u, v;
StringOps<num2> sops;
int get_nlength(const pair<int, int>& a) {
  if (a.first == u) {
    if (a.second != len1) {
      return len1 - 1;
    } else {
      return len1;
    }
  }
  if (a.second != len2) {
    return len2 - 1;
  } else {
    return len2;
  }
}
void fill_segs(int& l1, int& r1, int& l2, int& r2, const pair<int, int>& a) {
  if (a.first == u) {
    l2 = r2 = -1;
    if (a.second == 0) {
      l1 = 1, r1 = len1 - 1;
    } else if (a.second >= len1 - 1) {
      l1 = 0, r1 = a.second - 1;
    } else {
      l1 = 0, r1 = a.second - 1;
      l2 = a.second + 1, r2 = len1 - 1;
    }
    return;
  }
  l2 = r2 = -1;
  if (a.second == 0) {
    l1 = 1, r1 = len2 - 1;
  } else if (a.second >= len2 - 1) {
    l1 = 0, r1 = a.second - 1;
  } else {
    l1 = 0, r1 = a.second - 1;
    l2 = a.second + 1, r2 = len2 - 1;
  }
  l1 += len1, r1 += len1;
  if (l2 != -1) {
    l2 += len1, r2 += len1;
  }
}
int cmp2(vector<pair<int, int>>& a, vector<pair<int, int>>& b) {
  if (a.size() == 0 && b.size() > 0) {
    return -1;
  }
  if (a.size() == 0 && b.size() == 0) {
    return 0;
  }
  if (b.size() == 0 && a.size() > 0) {
    return 1;
  }
  if (a.size() == 1 && b.size() == 1 && a[0] == b[0]) {
    return 0;
  }
  if (a.size() == 2 && b.size() == 2 && a[0] == b[0] && a[1] == b[1]) {
    return 0;
  }
  if (sops.get(a[0].first) != sops.get(b[0].first)) {
    char c1 = sops.get(a[0].first);
    char c2 = sops.get(b[0].first);
    if (c1 < c2) {
      return -1;
    }
    return 1;
  }
  int lcp = sops.string_lcp(
      a[0].first, b[0].first,
      min(a[0].second - a[0].first + 1, b[0].second - b[0].first + 1));
  lcp = min({lcp, a[0].second - a[0].first + 1, b[0].second - b[0].first + 1});
  int nx = a[0].first + lcp - 1, ny = b[0].first + lcp - 1;
  vector<pair<int, int>> na, nb;
  if (nx >= a[0].second) {
    for (int i = 1; i < a.size(); ++i) {
      na.push_back(a[i]);
    }
  }
  if (ny >= b[0].second) {
    for (int i = 1; i < b.size(); ++i) {
      nb.push_back(b[i]);
    }
  }
  if (nx < a[0].second) {
    na.push_back(make_pair(nx + 1, a[0].second));
    for (int i = 1; i < a.size(); ++i) {
      na.push_back(a[i]);
    }
  }
  if (ny < b[0].second) {
    nb.push_back(make_pair(ny + 1, b[0].second));
    for (int i = 1; i < b.size(); ++i) {
      nb.push_back(b[i]);
    }
  }
  return cmp2(na, nb);
}
bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
  int nlen1 = get_nlength(a);
  int nlen2 = get_nlength(b);
  if (nlen1 == 0 && nlen2 == 0) {
    if (a.first == u & b.first == v) {
      return true;
    }
    return false;
  }
  if (nlen1 == 0) {
    return true;
  }
  if (nlen2 == 0) {
    return false;
  }
  int l1, r1, l2, r2;
  fill_segs(l1, r1, l2, r2, a);
  int p1, q1, p2, q2;
  fill_segs(p1, q1, p2, q2, b);
  vector<pair<int, int>> na;
  vector<pair<int, int>> nb;
  na.push_back(make_pair(l1, r1));
  if (l2 != -1) {
    na.push_back(make_pair(l2, r2));
  }
  nb.push_back(make_pair(p1, q1));
  if (p2 != -1) {
    nb.push_back(make_pair(p2, q2));
  }
  int val = cmp2(na, nb);
  if (val == 0) {
    if (a.first == u && b.first == v) {
      return true;
    }
    return false;
  }
  return val == -1;
}
bool cmpst(const pair<string, pair<int, int>>& a,
           const pair<string, pair<int, int>>& b) {
  if (a.first < b.first) {
    return true;
  }
  if (a.first > b.first) {
    return false;
  }
  if (a.first == b.first) {
    if (a.second.first == u && b.second.first == v) {
      return true;
    }
    return false;
  }
}
struct Node {
  Node() { left = right = nullptr; }
  int idxleft, idxright;
  Node* left;
  Node* right;
};
void dfs(Node* root, vector<int>& order) {
  if (!root) {
    return;
  }
  if (root->left) {
    dfs(root->left, order);
  }
  for (int i = root->idxleft; i <= root->idxright; ++i) {
    order.push_back(i);
  }
  if (root->right) {
    dfs(root->right, order);
  }
}
vector<pair<int, int>> get_order(const string& s) {
  int n = s.length();
  Node* root = new Node();
  Node* curnode = root;
  for (int i = 0; i < n;) {
    int cur = i;
    while (cur < n && s[cur] == s[i]) {
      ++cur;
    }
    curnode->idxleft = i, curnode->idxright = cur - 1;
    if (cur == n) {
      break;
    }
    if (s[i] < s[cur]) {
      Node* left = new Node();
      curnode->left = left;
      curnode = curnode->left;
    } else {
      Node* right = new Node();
      curnode->right = right;
      curnode = curnode->right;
    }
    i = cur;
  }
  vector<int> order;
  dfs(root, order);
  int ps = n;
  for (int i = 0; i < n; ++i) {
    if (order[i] == n - 1) {
      ps = i + 1;
      break;
    }
  }
  vector<pair<int, int>> result;
  for (int i = 0; i < ps; ++i) {
    result.push_back(make_pair(v, order[i]));
  }
  result.push_back(make_pair(v, n));
  for (int i = ps; i < n; ++i) {
    result.push_back(make_pair(v, order[i]));
  }
  return result;
}
int dp[2][maxN];
int dp2[2][maxN];
void gen() {
  cout << 2 << endl;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 500000; ++j) {
      cout << "a";
    }
    cout << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  for (int i = 0; i <= s[0].length(); ++i) {
    dp[u][i] = 1;
    dp2[u][i] = 1;
  }
  sops = StringOps<num2>(s[0]);
  len1 = 0, len2 = s[0].length();
  vector<pair<int, int>> order[2];
  u = 1, v = 0;
  order[0] = get_order(s[0]);
  swap(u, v);
  for (int i = 0; i + 1 < n; ++i) {
    string s1 = s[i];
    string s2 = s[i + 1];
    string nw = s1 + s2;
    len1 = s1.length(), len2 = s2.length();
    sops = StringOps<num2>(nw);
    order[v] = get_order(s[i + 1]);
    int cadd = 0;
    int head1 = 0, head2 = 0;
    while (head1 < order[u].size() || head2 < order[v].size()) {
      if (head1 == order[u].size()) {
        dp[v][order[v][head2].second] = cadd;
        ++head2;
      } else if (head2 == order[v].size()) {
        break;
      } else {
        if (cmp(order[u][head1], order[v][head2])) {
          cadd += dp[u][order[u][head1].second];
          if (cadd >= MOD) {
            cadd -= MOD;
          }
          ++head1;
        } else {
          dp[v][order[v][head2].second] = cadd;
          ++head2;
        }
      }
    }
    swap(u, v);
  }
  int res = 0;
  int res2 = 0;
  for (int i = 0; i <= s.back().length(); ++i) {
    res += dp[u][i];
    if (res >= MOD) {
      res -= MOD;
    }
    res2 += dp2[u][i];
    if (res2 >= MOD) {
      res2 -= MOD;
    }
  }
  cout << res << endl;
  cerr << (double)clock() / CLOCKS_PER_SEC << endl;
  return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

This is an easier version of the problem E with smaller 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 ≤ 1000): 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 more or equal than 1. The sum of lengths of words does not exceed 20000.

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).

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
struct num {
  static const int MA = 1e9 + 7, MB = 1e9 + 9;
  int a, b;
  num() {}
  num(int x) : a(x), b(x) {}
  num(int a, int b) : a(a), b(b) {}
  num operator+(const num& x) const {
    return num((a + x.a) % MA, (b + x.b) % MB);
  }
  num operator-(const num& x) const {
    return num((a + MA - x.a) % MA, (b + MB - x.b) % MB);
  }
  num operator*(int x) const {
    return num(((long long)a * x) % MA, ((long long)b * x) % MB);
  }
  num operator*(const num& x) const {
    return num(((long long)a * x.a) % MA, ((long long)b * x.b) % MB);
  }
  bool operator==(const num& x) const { return a == x.a && b == x.b; }
};
struct num2 {
  static const int MB = 1e9 + 9;
  int b;
  num2() {}
  num2(int x) : b(x) {}
  num2 operator+(const num2& x) const { return num2((b + x.b) % MB); }
  num2 operator-(const num2& x) const { return num2((b + MB - x.b) % MB); }
  num2 operator*(int x) const { return num2(((long long)b * x) % MB); }
  num2 operator*(const num2& x) const {
    return num2(((long long)b * x.b) % MB);
  }
  bool operator==(const num2& x) const { return b == x.b; }
};
const int MODP = 701;
template <class T>
struct StringOps {
  StringOps() {}
  StringOps(const string& s) : s(s) {
    n = s.length();
    deg.resize(n + 1);
    hashes.resize(n);
    deg[0] = T(1);
    for (int i = 1; i <= n; ++i) {
      deg[i] = deg[i - 1] * T(MODP);
    }
    T curhash = T(0);
    for (int i = 0; i < n; ++i) {
      curhash = curhash * T(MODP) + T(s[i]);
      hashes[i] = curhash;
    }
  }
  char get(int idx) { return s[idx]; }
  T get_hash(int l, int r) {
    T cur(0);
    if (l > 0) {
      cur = hashes[l - 1];
    }
    return hashes[r] - cur * deg[r - l + 1];
  }
  int string_lcp(int l, int r, int lim = 1e9) {
    int cleft = 1, cright = min(n - r, n - l);
    cright = min(lim, cright);
    int res = 0;
    while (cleft <= cright) {
      int key = (cleft + cright) / 2;
      if (get_hash(l, l + key - 1) == get_hash(r, r + key - 1)) {
        res = key;
        cleft = key + 1;
      } else {
        cright = key - 1;
      }
    }
    return res;
  }
  bool substring_cmp(int l1, int r1, int l2, int r2) {
    int lcp = string_lcp(l1, l2);
    int len1 = r1 - l1 + 1, len2 = r2 - l2 + 1;
    if (lcp >= len1 && lcp >= len2) {
      return len1 < len2;
    }
    if (lcp >= len1) {
      return true;
    }
    if (lcp >= len2) {
      return false;
    }
    return s[l1 + lcp] < s[l2 + lcp];
  }
  vector<T> hashes;
  vector<T> deg;
  string s;
  int n;
};
const int maxN = 1000100;
const int MOD = 1e9 + 7;
int len1, len2;
int u, v;
StringOps<num2> sops;
int get_nlength(const pair<int, int>& a) {
  if (a.first == u) {
    if (a.second != len1) {
      return len1 - 1;
    } else {
      return len1;
    }
  }
  if (a.second != len2) {
    return len2 - 1;
  } else {
    return len2;
  }
}
void fill_segs(int& l1, int& r1, int& l2, int& r2, const pair<int, int>& a) {
  if (a.first == u) {
    l2 = r2 = -1;
    if (a.second == 0) {
      l1 = 1, r1 = len1 - 1;
    } else if (a.second >= len1 - 1) {
      l1 = 0, r1 = a.second - 1;
    } else {
      l1 = 0, r1 = a.second - 1;
      l2 = a.second + 1, r2 = len1 - 1;
    }
    return;
  }
  l2 = r2 = -1;
  if (a.second == 0) {
    l1 = 1, r1 = len2 - 1;
  } else if (a.second >= len2 - 1) {
    l1 = 0, r1 = a.second - 1;
  } else {
    l1 = 0, r1 = a.second - 1;
    l2 = a.second + 1, r2 = len2 - 1;
  }
  l1 += len1, r1 += len1;
  if (l2 != -1) {
    l2 += len1, r2 += len1;
  }
}
int cmp2(vector<pair<int, int>>& a, vector<pair<int, int>>& b) {
  if (a.size() == 0 && b.size() > 0) {
    return -1;
  }
  if (a.size() == 0 && b.size() == 0) {
    return 0;
  }
  if (b.size() == 0 && a.size() > 0) {
    return 1;
  }
  if (a.size() == 1 && b.size() == 1 && a[0] == b[0]) {
    return 0;
  }
  if (a.size() == 2 && b.size() == 2 && a[0] == b[0] && a[1] == b[1]) {
    return 0;
  }
  if (sops.get(a[0].first) != sops.get(b[0].first)) {
    char c1 = sops.get(a[0].first);
    char c2 = sops.get(b[0].first);
    if (c1 < c2) {
      return -1;
    }
    return 1;
  }
  int lcp = sops.string_lcp(
      a[0].first, b[0].first,
      min(a[0].second - a[0].first + 1, b[0].second - b[0].first + 1));
  lcp = min({lcp, a[0].second - a[0].first + 1, b[0].second - b[0].first + 1});
  int nx = a[0].first + lcp - 1, ny = b[0].first + lcp - 1;
  vector<pair<int, int>> na, nb;
  if (nx >= a[0].second) {
    for (int i = 1; i < a.size(); ++i) {
      na.push_back(a[i]);
    }
  }
  if (ny >= b[0].second) {
    for (int i = 1; i < b.size(); ++i) {
      nb.push_back(b[i]);
    }
  }
  if (nx < a[0].second) {
    na.push_back(make_pair(nx + 1, a[0].second));
    for (int i = 1; i < a.size(); ++i) {
      na.push_back(a[i]);
    }
  }
  if (ny < b[0].second) {
    nb.push_back(make_pair(ny + 1, b[0].second));
    for (int i = 1; i < b.size(); ++i) {
      nb.push_back(b[i]);
    }
  }
  return cmp2(na, nb);
}
bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
  int nlen1 = get_nlength(a);
  int nlen2 = get_nlength(b);
  if (nlen1 == 0 && nlen2 == 0) {
    if (a.first == u & b.first == v) {
      return true;
    }
    return false;
  }
  if (nlen1 == 0) {
    return true;
  }
  if (nlen2 == 0) {
    return false;
  }
  int l1, r1, l2, r2;
  fill_segs(l1, r1, l2, r2, a);
  int p1, q1, p2, q2;
  fill_segs(p1, q1, p2, q2, b);
  vector<pair<int, int>> na;
  vector<pair<int, int>> nb;
  na.push_back(make_pair(l1, r1));
  if (l2 != -1) {
    na.push_back(make_pair(l2, r2));
  }
  nb.push_back(make_pair(p1, q1));
  if (p2 != -1) {
    nb.push_back(make_pair(p2, q2));
  }
  int val = cmp2(na, nb);
  if (val == 0) {
    if (a.first == u && b.first == v) {
      return true;
    }
    return false;
  }
  return val == -1;
}
bool cmpst(const pair<string, pair<int, int>>& a,
           const pair<string, pair<int, int>>& b) {
  if (a.first < b.first) {
    return true;
  }
  if (a.first > b.first) {
    return false;
  }
  if (a.first == b.first) {
    if (a.second.first == u && b.second.first == v) {
      return true;
    }
    return false;
  }
}
struct Node {
  Node() { left = right = nullptr; }
  int idxleft, idxright;
  Node* left;
  Node* right;
};
void dfs(Node* root, vector<int>& order) {
  if (!root) {
    return;
  }
  if (root->left) {
    dfs(root->left, order);
  }
  for (int i = root->idxleft; i <= root->idxright; ++i) {
    order.push_back(i);
  }
  if (root->right) {
    dfs(root->right, order);
  }
}
vector<pair<int, int>> get_order(const string& s) {
  int n = s.length();
  Node* root = new Node();
  Node* curnode = root;
  for (int i = 0; i < n;) {
    int cur = i;
    while (cur < n && s[cur] == s[i]) {
      ++cur;
    }
    curnode->idxleft = i, curnode->idxright = cur - 1;
    if (cur == n) {
      break;
    }
    if (s[i] < s[cur]) {
      Node* left = new Node();
      curnode->left = left;
      curnode = curnode->left;
    } else {
      Node* right = new Node();
      curnode->right = right;
      curnode = curnode->right;
    }
    i = cur;
  }
  vector<int> order;
  dfs(root, order);
  int ps = n;
  for (int i = 0; i < n; ++i) {
    if (order[i] == n - 1) {
      ps = i + 1;
      break;
    }
  }
  vector<pair<int, int>> result;
  for (int i = 0; i < ps; ++i) {
    result.push_back(make_pair(v, order[i]));
  }
  result.push_back(make_pair(v, n));
  for (int i = ps; i < n; ++i) {
    result.push_back(make_pair(v, order[i]));
  }
  return result;
}
int dp[2][maxN];
int dp2[2][maxN];
void gen() {
  cout << 2 << endl;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 500000; ++j) {
      cout << "a";
    }
    cout << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  for (int i = 0; i <= s[0].length(); ++i) {
    dp[u][i] = 1;
    dp2[u][i] = 1;
  }
  sops = StringOps<num2>(s[0]);
  len1 = 0, len2 = s[0].length();
  vector<pair<int, int>> order[2];
  u = 1, v = 0;
  order[0] = get_order(s[0]);
  swap(u, v);
  for (int i = 0; i + 1 < n; ++i) {
    string s1 = s[i];
    string s2 = s[i + 1];
    string nw = s1 + s2;
    len1 = s1.length(), len2 = s2.length();
    sops = StringOps<num2>(nw);
    order[v] = get_order(s[i + 1]);
    int cadd = 0;
    int head1 = 0, head2 = 0;
    while (head1 < order[u].size() || head2 < order[v].size()) {
      if (head1 == order[u].size()) {
        dp[v][order[v][head2].second] = cadd;
        ++head2;
      } else if (head2 == order[v].size()) {
        break;
      } else {
        if (cmp(order[u][head1], order[v][head2])) {
          cadd += dp[u][order[u][head1].second];
          if (cadd >= MOD) {
            cadd -= MOD;
          }
          ++head1;
        } else {
          dp[v][order[v][head2].second] = cadd;
          ++head2;
        }
      }
    }
    swap(u, v);
  }
  int res = 0;
  int res2 = 0;
  for (int i = 0; i <= s.back().length(); ++i) {
    res += dp[u][i];
    if (res >= MOD) {
      res -= MOD;
    }
    res2 += dp2[u][i];
    if (res2 >= MOD) {
      res2 -= MOD;
    }
  }
  cout << res << endl;
  cerr << (double)clock() / CLOCKS_PER_SEC << endl;
  return 0;
}

```