#include #include #include template class Node { public: Node(T *obj, int id, bool isDeleteAtDestractor = false) { this->obj = obj; this->id = id; this->delFlg = isDeleteAtDestractor; } ~Node() { if(this->delFlg && this->obj) { delete this->obj; } } protected: T *obj; int id; bool delFlg; public: T* get() { return this->obj; } int getId() { return this->id; } }; template class Edge { public: Edge(Node *a, Node *b, int id) { this->a = a; this->b = b; this->id = id; } public: Node *a; Node *b; protected: int id; public: int getId() { return this->id; } }; template class Graph { public: Graph() { this->nodeId = -1; this->edgeId = -1; this->rootId = -1; } virtual ~Graph() { for(auto i = this->nodes.begin(); i != this->nodes.end(); i++) { delete (*i); } for(auto i = this->edges.begin(); i != this->edges.end(); i++) { delete (*i); } } protected: std::vector*> nodes; std::vector*> edges; int rootId; int nodeId; int edgeId; public: virtual int addNode(T obj) { this->nodeId++; T *o = new T; *o = obj; this->nodes.push_back(new Node(o, this->nodeId, true)); return this->nodeId; } virtual int addEdge(int a, int b) { this->edgeId++; this->edges.push_back(new Edge(this->nodes[a], this->nodes[b], this->edgeId)); return this->edgeId; } virtual Node* getNode(int id) { return this->nodes[id]; } virtual Edge* getEdge(int id) { return this->edges[id]; } virtual std::vector getNodeIds() { std::vector res; for(auto i = this->nodes.begin(); i != this->nodes.end(); i++) { if(!(*i)) continue; res.push_back((*i)->getId()); } return res; } virtual std::vector getEdgeIds() { std::vector res; for(auto i = this->edges.begin(); i != this->edges.end(); i++) { if(!(*i)) continue; res.push_back((*i)->getId()); } return res; } virtual void removeNode(int id) { Node *obj = this->nodes[id]; delete obj; this->nodes[id] = nullptr; } virtual void removeEdge(int id) { Edge *obj = this->edges[id]; delete obj; this->edges[id] = nullptr; } virtual int countNode() { return this->getNodeIds().size(); } virtual int countEdge() { return this->getEdgeIds().size(); } virtual void setRoot(int root) { this->rootId = root; } virtual int getRoot() { return this->rootId; } virtual std::vector getChildNode(int nodeId) = 0; virtual std::vector getChildEdge(int nodeId) = 0; }; template class DirectedGraph: public Graph { public: virtual std::vector getChildNode(int nodeId) { std::vector res; std::vector ids = this->getEdgeIds(); for(auto i = ids.begin(); i != ids.end(); i++) { if(nodeId != this->getEdge(*i)->a->getId()) continue; res.push_back(this->getEdge(*i)->b->getId()); } return res; } virtual std::vector getChildEdge(int nodeId) { std::vector res; std::vector ids = this->getEdgeIds(); for(auto i = ids.begin(); i != ids.end(); i++) { int id = this->getEdge(*i)->a->getId(); if(nodeId != id) continue; res.push_back(this->getEdge(*i)->getId()); } return res; } }; int checkAdjacent(char a, char b) { static std::map m; if(m.empty()) { m.insert(std::make_pair('P', 'D')); m.insert(std::make_pair('D', 'C')); m.insert(std::make_pair('C', 'A')); m.insert(std::make_pair('A', '\0')); } if(m[a] == b) return 1; if(m[b] == a) return -1; return 0; } int main(void) { DirectedGraph g; g.setRoot(g.addNode('0')); int n, m; std::cin >> n >> m; char *s = new char[n + 1]; std::cin >> s; s[n] = 0; for(int i = 0; s[i]; i++) { g.addNode(s[i]); } delete s; s = nullptr; for(int i = 0; i < m; i++) { int u, v; std::cin >> u >> v; switch(checkAdjacent(*g.getNode(u)->get(), *g.getNode(v)->get())) { case 1: g.addEdge(u, v); if(*g.getNode(u)->get() == 'P') g.addEdge(g.getRoot(), u); break; case -1: g.addEdge(v, u); if(*g.getNode(v)->get() == 'P') g.addEdge(g.getRoot(), v); break; default:; } } long long ans = 0; auto pl = g.getChildNode(g.getRoot()); for(auto p = pl.begin(); p != pl.end(); p++) { auto dl = g.getChildNode(g.getNode(*p)->getId()); for(auto d = dl.begin(); d != dl.end(); d++) { auto cl = g.getChildNode(g.getNode(*d)->getId()); for(auto c = cl.begin(); c != cl.end(); c++) { ans += g.getChildNode(g.getNode(*c)->getId()).size(); ans %= 1000000007; } } } std::cout << ans << std::endl; }