extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign, Range};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet, HashMap};
use std::fmt::Display;

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}

macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}

macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
#[derive(Copy, Clone)]
struct Function {
    a: i32, b: usize, c: usize
}
fn can_make(sequence: &Vec<i32>, functions: &Vec<Function>, func: usize, from: usize, until: usize, memo: &mut Vec<Vec<Vec<Option<bool>>>>) -> bool {
    //println!("func: {}, from: {}, until: {}", func, from, until);
    if let Some(cache) = memo[func][from][until - from] {
        cache
    }else {
        let result = if from + 1 == until {
            functions[func].a == sequence[from]
        }else {
            let mut res = false;
            for mid in from + 1 .. until {
                if can_make(sequence, functions, functions[func].b, from, mid, memo) &&
                    can_make(sequence, functions, functions[func].c, mid, until, memo) {
                    res = true;
                    break
                }
            }
            res
        };
        memo[func][from][until - from] = Some(result);
        result
    }
}
fn main() {
    let n: usize = read_value!();
    let sequence: Vec<i32> = read_line!(' '; i32);
    let m: usize = read_value!();
    let mut functions = Vec::with_capacity(m);
    for _ in 0 .. m {
        let_all!(a: i32, b: usize, c: usize);
        functions.push(Function{a: a, b: b - 1, c: c - 1});
    }
    let mut memo = vec![vec![vec![None; n + 1]; n]; m];
    println!("{}", if can_make(&sequence, &functions, 0, 0, n, &mut memo) {"Yes"} else {"No"});

}




Measured memory_bytes: 5048