#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 123;
const int L = 11;
const int MAX = 20;
const int mod = 998244353;
int n;
void add(int &a, int b) {
  a += b;
  if (a >= mod) a %= mod;
}
int sum(int a, int b) {
  add(a, b);
  return a;
}
int mult(int a, int b) { return 1ll * a * b % mod; }
int inv[N], first[N], pw3[N];
int cnk(int n, int k) {
  int answer = mult(first[n], inv[k]);
  answer = mult(answer, inv[n - k]);
  return answer;
}
int bp(int a, long long n) {
  int answer = 1;
  while (n) {
    if (n & 1) answer = mult(answer, a);
    a = mult(a, a);
    n >>= 1;
  }
  return answer;
}
int calc(int x, int y) {
  if (y == 0) swap(x, y);
  if (x == 0) {
    return mult(bp(3, y), bp(3, 1ll * n * (n - y)));
  }
  return mult(3, bp(3, 1ll * (n - x) * (n - y)));
}
void solve() {
  cin >> n;
  first[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    first[i] = mult(first[i - 1], i);
    inv[i] = bp(first[i], mod - 2);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int temp = mod - bp(3, i);
    int now = mult(cnk(n, i), sum(bp(sum(1, temp), n), mod - bp(temp, n)));
    if (i & 1)
      add(ans, now);
    else
      add(ans, mod - now);
  }
  ans = mult(ans, 3);
  for (int i = 1; i <= n; i++) {
    int j = 0;
    int now = mult(cnk(n, i), cnk(n, j));
    now = mult(now, calc(i, j));
    if ((i + j) & 1)
      add(ans, now);
    else
      add(ans, mod - now);
  }
  for (int j = 1; j <= n; j++) {
    int i = 0;
    int now = mult(cnk(n, i), cnk(n, j));
    now = mult(now, calc(i, j));
    if ((i + j) & 1)
      add(ans, now);
    else
      add(ans, mod - now);
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio();
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
