#include <bits/stdc++.h>
using namespace std;
const int ha = 998244353, N = 1e6 + 5, mod = ha - 1;
inline int add(int x, int y) {
  x += y;
  return x >= ha ? x - ha : x;
}
inline void ADD(int &x, int y) {
  x += y;
  if (x >= ha) x -= ha;
}
inline int ksm(int x, int y) {
  int an = 1;
  for (; y; y >>= 1, x = x * (long long)x % ha)
    if (y & 1) an = an * (long long)x % ha;
  return an;
}
int n, jc[N], ni[N], f[N], ans;
inline void init() {
  jc[0] = 1;
  for (int i = 1; i <= n; i++) jc[i] = jc[i - 1] * (long long)i % ha;
  ni[n] = ksm(jc[n], ha - 2);
  for (int i = n; i; i--) ni[i - 1] = ni[i] * (long long)i % ha;
}
inline int C(int x, int y) {
  return jc[x] * (long long)ni[y] % ha * (long long)ni[x - y] % ha;
}
inline void solve() {
  ans = ksm(3, n * (long long)n % mod);
  for (int i = 1, now; i <= n; i++) {
    now = C(n, i) * (long long)ksm(3, (i + n * (long long)(n - i)) % mod) % ha;
    if (i & 1)
      ADD(ans, ha - add(now, now));
    else
      ADD(ans, add(now, now));
  }
  int tot = 0;
  for (int i = 1; i <= n; i++)
    if (n - i & 1)
      ADD(tot,
          ha - C(n, i) *
                   (long long)add(
                       ksm(add(1, ha - ksm(3, n - i)), n),
                       (n & 1) ? ksm(3, n * (long long)(n - i) % mod)
                               : ha - ksm(3, n * (long long)(n - i) % mod)) %
                   ha);
    else
      ADD(tot, C(n, i) *
                   (long long)add(
                       ksm(add(1, ha - ksm(3, n - i)), n),
                       (n & 1) ? ksm(3, n * (long long)(n - i) % mod)
                               : ha - ksm(3, n * (long long)(n - i) % mod)) %
                   ha);
  ADD(ans, tot * 3ll % ha);
  ans = add(ha - ans, ksm(3, n * (long long)n % mod));
}
int main() {
  scanf("%d", &n), init();
  solve(), printf("%d\n", ans);
  return 0;
}
