#include <bits/stdc++.h>   using namespace std;   struct Aho {     Aho *son[26];     Aho *fail;     int cntstr, faildp;     Aho() {       for (int i = 0; i < 26; ++i) son[i] = NULL;       fail = NULL;       cntstr = 0;       faildp = -1;     }   };   void insert(Aho *x, int i, const string &str) {     if (i < str.size()) {       if (x->son[str[i] -  a ] == NULL) {         x->son[str[i] -  a ] = new Aho();       }       insert(x->son[str[i] -  a ], i + 1, str);     } else {       x->cntstr++;     }   }   void calcFailEdges(Aho *root) {     deque<Aho *> q;     root->fail = root;     q.push_back(root);     while (!q.empty()) {       Aho *x = q.front();       q.pop_front();       for (int i = 0; i < 26; ++i) {         if (x->son[i] != NULL) {           q.push_back(x->son[i]);           Aho *pnt = x->fail;           if (x == root)             x->son[i]->fail = root;           else {             while (pnt != root && pnt->son[i] == NULL) {               pnt = pnt->fail;             }             if (pnt->son[i] != NULL)               x->son[i]->fail = pnt->son[i];             else               x->son[i]->fail = root;           }         }       }     }   }   int getDp(Aho *t) {     if (t->faildp == -1) {       t->faildp = t->cntstr;       t->faildp += getDp(t->fail);     }     return t->faildp;   }   const int MAX_N = 200000;   Aho *state[1 + MAX_N];   Aho *advance(Aho *t, char ch) {     while (t->fail != t && t->son[ch -  a ] == NULL) t = t->fail;     if (t->son[ch -  a ] == NULL) return t;     return t->son[ch -  a ];   }   Aho *state1[MAX_N], *state2[MAX_N];   void doStuff(int n, Aho **state, const string &str, const vector<string> &vec) {     Aho *T = new Aho();     for (int i = 0; i < n; ++i) insert(T, 0, vec[i]);     T->faildp = T->cntstr;     calcFailEdges(T);     Aho *pnt = T;     for (int i = 1; i <= str.size(); ++i) {       pnt = advance(pnt, str[i - 1]);       state[i] = pnt;     }   }   int main() {     int n;     string str;     cin >> str >> n;     vector<string> vec;     for (int i = 0; i < n; ++i) {       string p;       cin >> p;       vec.push_back(p);     }     doStuff(n, state1, str, vec);     for (int i = 0; i < n; ++i) reverse(vec[i].begin(), vec[i].end());     reverse(str.begin(), str.end());     doStuff(n, state2, str, vec);     long long rez = 0LL;     for (int i = 1; i < str.size(); ++i)       rez = (long long)getDp(state1[i]) * getDp(state2[str.size() - i]) + rez;     printf( %lld , rez);     return 0;   }   