package net.ipipip0129.yukicoder.no762; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String[] split_str = scan.nextLine().split(" "); int leaf_num = Integer.parseInt(split_str[0]); int pass_num = Integer.parseInt(split_str[1]); PDCALeaf[] pdcaLeaves = new PDCALeaf[leaf_num]; String pdca_str = scan.nextLine(); for (int i = 0; i < leaf_num; i++) { switch (pdca_str.charAt(i)) { case 'P': pdcaLeaves[i] = new PDCALeaf(PDCA.Plan, 0); break; case 'D': pdcaLeaves[i] = new PDCALeaf(PDCA.Do, 0); break; case 'C': pdcaLeaves[i] = new PDCALeaf(PDCA.Check, 0); break; case 'A': pdcaLeaves[i] = new PDCALeaf(PDCA.Act, 1); break; } } for (int i = 0; i < pass_num; i++) { split_str = scan.nextLine().split(" "); int a_num = Integer.parseInt(split_str[0]) - 1; int b_num = Integer.parseInt(split_str[1]) - 1; PDCA a_type = pdcaLeaves[a_num].getType(); PDCA b_type = pdcaLeaves[b_num].getType(); if ((a_type == PDCA.Act && b_type == PDCA.Check) || (a_type == PDCA.Check && b_type == PDCA.Do) || (a_type == PDCA.Do && b_type == PDCA.Plan)) { pdcaLeaves[b_num].addLeaf(a_num); } else if ((a_type == PDCA.Check && b_type == PDCA.Act) || (a_type == PDCA.Do && b_type == PDCA.Check) || (a_type == PDCA.Plan && b_type == PDCA.Do)) { pdcaLeaves[a_num].addLeaf(b_num); } } PDCA[] check_array = new PDCA[] {PDCA.Check, PDCA.Do, PDCA.Plan}; for (int j = 0; j < 3; j++) { for (int i = 0; i < leaf_num; i++) { if (pdcaLeaves[i].getType() == check_array[j]) { long leaf_weight = pdcaLeaves[i].getWeight(); for (int leaf : pdcaLeaves[i].getLeaf_list()) { leaf_weight += pdcaLeaves[leaf].getWeight(); } pdcaLeaves[i].setWeight(leaf_weight); } } } long ans = 0; for (int i = 0; i < leaf_num; i++) { if (pdcaLeaves[i].getType() == PDCA.Plan) { ans += pdcaLeaves[i].getWeight(); } } System.out.println(ans % 1000000007); scan.close(); } } class PDCALeaf { PDCA type_pdca; long weight; List leaf_list; PDCALeaf(PDCA type, int weight) { this.type_pdca = type; this.weight = weight; this.leaf_list = new ArrayList<>(); } void addLeaf(int leaf_num) { leaf_list.add(leaf_num); } PDCA getType() { return type_pdca; } List getLeaf_list() { return leaf_list; } long getWeight() { return weight; } public void setWeight(long weight) { this.weight = weight; } } enum PDCA { Plan, Do, Check, Act }