#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, n[100005];
  cin >> a;
  int mx = 0, maxn = 0, x = 0, y = 0, sn = 1e8, sm = 1e8;
  int ansa = 0, ansb = 0;
  for (int i = 0; i < a; i++) {
    cin >> n[i];
    if (n[i] == 1) {
      ansa++;
      ansb = 0;
      x++;
    } else {
      ansb++;
      ansa = 0;
      y++;
    }
    mx = max(mx, ansa);
    maxn = max(maxn, ansb);
  }
  if ((mx > 0 && a % mx != 0) || (maxn > 0 && a % maxn != 0))
    return cout << "NO", 0;
  if (mx == 0 || maxn == 0) return cout << "YES", 0;
  if (mx != maxn) return cout << "NO", 0;
  if (mx == maxn && x == y) return cout << "YES", 0;
  if (mx == maxn && x != y)
    if (x - mx == y || y - maxn == x) return cout << "YES", 0;
  cout << "NO";
  return 0;
}
